supabase/gotrue-dart

Persistant user session

atnegrete opened this issue ยท 11 comments

Feature request

Persist the user's session upon application reload.

I could very well be implementing auth wrong, but opening request in case I'm not.

Describe the solution you'd like

When a user is signed in and leave->reload the application, they should not be prompted to login. Speaking specifically for flutter, a reload prompts back to sign in.

Describe alternatives you've considered

Storing token in the device, and silent refresh token as needed.

Additional context

I would be more than happy to create a PR with changes if given some guidance on implementation. Mobile authentication is not my strong suit.

When a user is signed in and leave->reload the application, they should not be prompted to login.

Hi @atnegrete can you share your code? Maybe i can help.

You can also take a look at this example. I use a splashScreen to manage authentication on app startup. instead of sending user to signin screen, you can send them to a landing page for example.

Storing token in the device, and silent refresh token as needed.

gotrue-dart doesn't provide storage. But you can use any Flutter storage libraries to do that.
In this example, i use shared_preferences

Yeah, what you have in the example is exactly what I'm asking for. Given that the web version of supabase handles the persist logic internally (I think from looking at the source code), basically what I'm asking is if we can go ahead and essentially add your _restoreSession logic into the source code, without the redirecting logic. I'd argue that would be better placed in a supbase-dart-ui components repository with auth components?

We should default to persistence being on by default, in the spirit of being the Firebase Open Source alternative? Similar to https://firebase.google.com/docs/auth/web/auth-state-persistence

Possible method to override:
SupabaseClient(url, key)..auth.setPersistance(Persistance.Local)

Side note, Thanks a ton for your contribution, loving using these libraries!

We should default to persistence being on by default, in the spirit of being the Firebase Open Source alternative?

Originally, we want to keep this library as a dart package. So there's no storage feature included.

Welcome for PR if you have a solution. That will be great.

I totally get that! I'm thinking that since https://pub.dev/packages/shared_preferences is supported pretty well across platforms, it should be ok?

I totally get that! I'm thinking that since https://pub.dev/packages/shared_preferences is supported pretty well across platforms, it should be ok?

shared_preferences is a flutter package

Ah, sorry I misunderstood your earlier response, I didn't realize you meant that originally & currently we want to keep it as dart package only.

I'll do some research, but most of my experience with dart is only for flutter - as I imagine 99% of everyone out there will be? I'll look & see if I can find a dart only solution

@phamhieu
This looks promising to me, what's your thoughts? https://pub.dev/packages/stash_sembast/versions/2.0.0-nullsafety.2

Wow, it looks promising. Nice find! @atnegrete

I'm storing string using FlutterSecureStorage. Although after a while recoverSession returns Invalid Refresh Token, I can't find what is causing that. Does this method work after expiration? Thanks.

Hi @difelice, can you check if recoverSession is called with the same refreshToken more than once? cos that will trigger Invalid Refresh Token error.

If it's not the case, please open a new issue. Thank you.

@difelice we released a new supabase-flutter package to help with 3rd party auth and user session persistence. Now you can use it with FlutterSecureStorage. More details here