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
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.What would be a solution to this?
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.I used the method 'setSession()' and passed the parameter refresh token, but It didn't work for some reason
You'd pass both of them. Can you share the code you're using for setSession?
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
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)Because the completion showed me that it only has one parameter (refreshToken)
Oh, interesting...they do just have the one. Works way different that the JS version.
Kind of weird
Then they take that refresh token and grab a new session; and I assume they then save it.
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
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.OK, thank you very much
I'd go ahead and show any errors you're getting from your calls.
{
"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
Awesome!