K
Kinde12mo ago
MaTTHD

kinde-flutter-sdk invalid session on every login attempt.

Hello! I have been trying to get the kinde flutter sdk to work, however for some reason after successfully logging in with the following code:
try {
final sdk = KindeFlutterSDK.instance;

final String? token = await sdk.login();
final otherToken = await sdk.getToken();

if (token != null) {
print(token);

final user = await sdk.getUserProfileV2();

print(user);
}
else {
print("Null token");
throw Exception('Token is null');
}

notifyListeners();
} catch (e) {
print('Error on login: $e');
}
}
try {
final sdk = KindeFlutterSDK.instance;

final String? token = await sdk.login();
final otherToken = await sdk.getToken();

if (token != null) {
print(token);

final user = await sdk.getUserProfileV2();

print(user);
}
else {
print("Null token");
throw Exception('Token is null');
}

notifyListeners();
} catch (e) {
print('Error on login: $e');
}
}
I just receive the following error: KindeError: Session expired or invalid and I am unsure why! This is my first time using Kinde so any help is greatly appreciated!
10 Replies
MaTTHD
MaTTHDOP12mo ago
Update, seems to work if my nextjs app and flutter app are not sharing the same Kinde app However, now I need to somehow have my nextjs getServerSession auth the users request ot the backend api which I think can be achieved by using the API's just not sure how
Oli - Kinde
Oli - Kinde12mo ago
Hey @MaTTHD, Great to hear you solved your initial issue.
However, now I need to somehow have my nextjs getServerSession auth the users request ot the backend api which I think can be achieved by using the API's just not sure how
It sounds like you're trying to authenticate requests from your Next.js app to your backend API using Kinde. Here's a general approach you can take: 1. When a user logs in, your Next.js app should receive a session token from Kinde. This token is used to authenticate the user's session. 2. When making requests to your backend API, include this session token in the Authorization header of the request. The header should look something like this: Authorization: Bearer <session_token>. 3. On your backend API, validate the session token in the Authorization header of incoming requests. You can use the getKindeServerSession function from the Kinde Next.js SDK to do this. Here's an example:
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";

export async function GET() {
const { getUser, isAuthenticated } = getKindeServerSession();

if (await !isAuthenticated()) {
return new Response("Unauthorized", { status: 401 });
}
const user = await getUser();
const data = { message: "Hello User", id: user.id };

return NextResponse.json({ data });
}
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";

export async function GET() {
const { getUser, isAuthenticated } = getKindeServerSession();

if (await !isAuthenticated()) {
return new Response("Unauthorized", { status: 401 });
}
const user = await getUser();
const data = { message: "Hello User", id: user.id };

return NextResponse.json({ data });
}
In this example, getKindeServerSession is used to check if the user is authenticated. If they are, the request is allowed to proceed. If not, a 401 Unauthorized response is returned. Please let me know if you need anymore help!
MaTTHD
MaTTHDOP12mo ago
Hey @Oli - Kinde thanks for the comment! I appreciate the help as I am just figuring all this out. I will provide a bit more context as that is not quite what I am after. - I have a flutter mobile application running which has Authentication / Login handled using Kinde. - I have a NextJS web application, for the web version of this product also running on localhost:3000 which has frontend and authentication ALSO with Kinde. The problem I am running into is the NextJS application has backend API routes as you showed there and I can call that as expected from anywhere inside the NextJS app, however if I try to call that same route from my mobile application using a post request, the session seems to be null
Oli - Kinde
Oli - Kinde12mo ago
Gotcha, I think this question is for one of my expert NextJS/Flutter team mates. I have passed on your question to my teammates. I am sure we can help you out here.
MaTTHD
MaTTHDOP12mo ago
That'd be awesome, this has been several 12 hour days for me trying to make something work!
Oli - Kinde
Oli - Kinde12mo ago
Sorry to hear that, we don't want you to waste time attempting to fix issues like this when we can help you save those several 12 hours of time. I'll get back to you soon.
MaTTHD
MaTTHDOP12mo ago
Appreciate it 😄 Last note, I think what I’ll end up doing is adding middleware to the nextjs app, if the request is coming from my app it will validate the JWT against the .jwks and let the traffic through But if there are other suggestions / more secure ways your team thinks of please let me know 🙂 As this would allow api access however the user would still be required to “sign up” twice if I’m not mistaken Any updates ?
Oli - Kinde
Oli - Kinde12mo ago
Hey @MaTTHD, I will get an update for you on this on Monday. Apologies for the delay
MaTTHD
MaTTHDOP12mo ago
Sounds good I managed to bootstrap something with auth0 for now but would prefer to try kinde
Oli - Kinde
Oli - Kinde12mo ago
Hey @MaTTHD, We are still looking into this. I will give you another update tomorrow. Hey @MaTTHD, This is taking longer than anticipated. We are still looking into your issue. I will get back to you once we hvae more information to share. Hey @MaTTHD, We have hit a bit of a point where we need more information to investigate your issue. The only thing that's jumping out to us, is the following. You should ensure that you are defining a GET function in your NextJS API routes then have a GET request from Flutter too. If you are trying to do a POST request when GET is defined in NextJS it won’t work. You explained your setup and issue here: https://discord.com/channels/1070212618549219328/1182206340962201600/1182216933555322880 However, we need more information to understand the state you are trying to achieve. Are you able to: 1. Explain more on what you are trying achieve? 2. Provide some code snippets/information of where you are running into this error
Want results from more Discord servers?
Add your server