S
Supabase3mo ago
Vini

Login by JWT Token (Access Token, Refresh Token) in Dart / Flutter

I'm creating a system, where the user log in the phone and scan a qr code from the phone camera and transfer the auth login to the PC version. My ideia is to do that by passing the JWT Token to the PC. Now, the problem is, I didn't find any way to log in by JWT token, access token or refresh token. Is there a code in dart to do that by the supabase lib? I have tried 'supabase.auth.setSession(), supabase.auth.refreshToken()', any of these didn't work.
17 Replies
j4
j43mo ago
I'm not familiar with the Dart/Flutter sdk, but setSession only authenticates your calls to Supabase, it does not allow things like getSession and getUser to work.
Vini
ViniOP3mo ago
What would be a solution to this?
j4
j43mo ago
Um... foolish me, I was wrong. setSession DOES save the session; so getSession and getUser should be working. I must've been getting myself confused with just setting the JWT in the global header.
Vini
ViniOP3mo ago
I used the method 'setSession()' and passed the parameter refresh token, but It didn't work for some reason
j4
j43mo ago
You'd pass both of them. Can you share the code you're using for setSession?
Vini
ViniOP3mo ago
ok Future logarPorTokens(String accessToken, String refreshToken) async { // Add your function code here! final supabase = SupaFlow.client; await supabase.auth.setSession(refreshToken) } The method I've created in dart
j4
j43mo ago
I'd have to lookup how their SDK works, but you at least need the accessToken in there too. The javascript sdk has you do this: setSession({ access_token, refresh_token }), but again, I'd have to lookup docs to see if the sdk you're using requires it to be inside of an object or if it just takes two args (accessToken, refreshToken)
Vini
ViniOP3mo ago
Because the completion showed me that it only has one parameter (refreshToken)
j4
j43mo ago
Oh, interesting...they do just have the one. Works way different that the JS version.
final refreshToken = supabase.currentSession?.refreshToken ?? '';
final AuthResponse response = await supabase.auth.setSession(refreshToken);
final session = res.session;
final refreshToken = supabase.currentSession?.refreshToken ?? '';
final AuthResponse response = await supabase.auth.setSession(refreshToken);
final session = res.session;
Vini
ViniOP3mo ago
Kind of weird
j4
j43mo ago
Then they take that refresh token and grab a new session; and I assume they then save it.
Vini
ViniOP3mo ago
My code to grab the refresh: Future<String?> pegarRefreshToken() async { // Add your function code here! final supabase = SupaFlow.client; final session = supabase.auth.currentSession; return session?.refreshToken; } I did it by refresh and it fails to run the api call I'll show the error the refresh token is way smaller than the access token? because the result of it to me was something like: A5FDADSF this size something like this
j4
j43mo ago
You'll have to be careful with your strategy here, because there's a chance that by the time you call setSession the Supabase client elsewhere may have already tried to use it to refresh the token. I've gotta go for now.
Vini
ViniOP3mo ago
OK, thank you very much
j4
j43mo ago
I'd go ahead and show any errors you're getting from your calls.
Vini
ViniOP3mo ago
{ "code": "refresh_token_not_found", "message": "Invalid Refresh Token: Refresh Token Not Found" } the error i got what if i do by api? I have solved it by using recoverSession and a session json string Thanks
j4
j43mo ago
Awesome!

Did you find this page helpful?