Skip to content

Not possible to keep user signed in #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mousindev opened this issue Apr 15, 2022 · 20 comments
Closed

Not possible to keep user signed in #323

mousindev opened this issue Apr 15, 2022 · 20 comments
Labels
auth bug Something isn't working p3 Priority 3

Comments

@mousindev
Copy link

Bug report

Describe the bug

The issues is that the user gets kicked off the app and needs to sign in again over and over.
After checking the many threads on this issue, these are the two scenarios we face:

  1. The token gets refreshed when I come back to the application after 1 hour which is the default expiration time (see screenshot below where the event is captured), but the immediate next call (and all subsequent ones) fail with the error message "JWT expired"
    image

  2. If I take way longer to come back to the application, the token does not get refreshed an the next request fails with a "invalid_grant Invalid Refresh Token" error.

To Reproduce

The project is using supabase-js and Angular installed as PWA

  1. Create the SupabaseClient with autoRefreshToken set to true.
  2. Sign in a user with supabase.auth.signIn({ email: email, password: password });
  3. Leave the application idle for a bit more than 1 hour for the first case described, or for several hours for the second one

Expected behavior

We are porting from Firebase to Supabase, and our understanding was that the autoRefreshToken should help achieving keeping the user signed in.
There are some other threads where it is proposed to check the result of every call, and if the error is "JWT expired" then try to sign in the user again in the background and retry the original call, but that would be a workaround more than the solution.

System information

  • Version of supabase-js: 1.33.3

Additional context

Just to say that we are loving Supabase. It would be great to be able to keep our users signed in automatically, otherwise it will be hard to deploy the version backed by Supabase rather than Firebase. Thanks!

@kiwicopple
Copy link
Member

Hey @nereolopez - just a quick check: what platform/framework are you using?

@mousindev
Copy link
Author

Hey @kiwicopple , thanks for your message. We are using Angular's latest version and Supabase directly

@mousindev
Copy link
Author

Hi @kiwicopple, just wanted to check if there is any progress on this? I personally don't know where the issue is coming from, but if somehow we could support or contribute, we can try with some guidance. Thanks!

@kiwicopple
Copy link
Member

This one is getting worked on over here! #274

@mousindev
Copy link
Author

Great news! I will keep an eye on it and test it as soon it is available, both in the browser and in Android/iOS installed as a PWA. Thanks!

@mousindev
Copy link
Author

Hi @kiwicopple, we noticed something additional (not sure if related, but in case it gives any hint).

When opening the App (be it on the browser or on the phone as a PWA) we are automatically redirected to the Sign in page. This is because the Angular guard that checks that route makes the user stay in the Sign In page if there is no supabase.auth.user(), otherwise redirects the user to the home page. The strange thing there is that if I look at the LocalStorage I can see the supabase.auth.token entry, and, if I reload the page (or pull to reload on the PWA) then the next time the guard checks the supabase.auth.user() is no longer null and the user gets redirected to the home page directly.
As said, this is not always the case due to what we discussed prior in this thread that the session is not always kept alive.

It is a strange behavior and can't see why, but hope the info is helpful.

Thanks

@lebrinkma
Copy link

@nereolopez this sounds like the issue I tried to solve with #265, but I have not received any feedback on the PR so far.
supabase.auth.user() returns null if the session is currently refreshing after the token expired.

@anngbaum
Copy link

@nereolopez @lebrinkma we are also encountering the same challenge (React Native app on iOS) and my understanding from the comment here was that it's likely due to our use of AsyncStorage. between the token expiring from background + user being null on first load, it's been a bit difficult to ensure we're using the correct user auth. would love to see a solution like #265 go out!

@mousindev
Copy link
Author

@anngbaum @lebrinkma, apologies for my delay, business trips are keeping me busy. Interesting the comment you linked, most likely it is scenario 3 which we are facing. The proposed solution supabase/gotrue-js#265 looks good. The only point I see is that then we have user and refreshedUser, and what this last one does (waiting in case there is a need of refresh and then return it) is what I would expect from the user directly. So not sure if it would be better to have that behavior inside the user itself. What do you think?

Also, I see that supabase/gotrue-js#274 that @kiwicopple mentioned is already closed. Is this available now if we update supabase-js version? Does it fix the problem we are describing?

Thanks to all!

@mousindev
Copy link
Author

Quick update. I just tried by updating to the latest version in case changes were already there, and still face the same. Angular Guard is redirecting the user to the home page because session and user are null, but if I refresh, then it enters directly into the application.

Is there any workaround in the meantime that could be taken?

@anngbaum
Copy link

anngbaum commented May 19, 2022

this was a helpful comment illustrating the delay, which I agree is still a problem for us as well.

the workaround that we're using is a listener onAuthStateChange which does successfully fire once the token is refreshed, and then using that to update the UI so that the user is taken directly into the application. It does cause an unfortunate flicker in the UI which is suboptimal, but at least prevents having to re-log in.

I asked a somewhat similar question in the #gotrue discord here but have not heard anything further, will let you know if that generates any responses though!

@J0
Copy link
Contributor

J0 commented Jul 18, 2022

Hey team,

Thanks for your patience -- we haven't forgotten about this issue and we'll be transferring this to the auth repo so we can best track this.
Thanks!

@anngbaum
Copy link

@J0 I was optimistic that the changes described in PR #285 would alleviate this problem (for us, at least). I have not updated our repo to test the go-true/next branch yet, but if there's any approximate timeline for that release, we would definitely still love to see this handling improved.

@alaister
Copy link
Member

Hey @anngbaum,
You can try out this version of gotrue-js by running npm install @supabase/supabase-js@next as it's included there.
It may be a little difficult to upgrade without any docs, so I'd recommend leaning heavily on the typescript typings.

We're aiming to get v2 out next month! 🤞🏻

@ARMATAV
Copy link

ARMATAV commented Aug 16, 2022

https://supabase.com/blog/supabase-js-v2

This solves the “getting logged out” issue, which has been a recurring challenge in our GitHub Discussions.

Should be good to go here now, right?

@mousindev
Copy link
Author

Hi @ARMATAV, that would be good news. Please allow us few days to test it and revert :)

@mousindev
Copy link
Author

@ARMATAV we've been testing with RC3 both on web and installed on phones as PWA in different scenarios and in all the cases the session kept open, first calls to backend after wake up of the app succeeded, so for now, not able to reproduce the issue 🥳.

Please feel free to close it and thanks again for the fix and support!

@ARMATAV
Copy link

ARMATAV commented Aug 21, 2022

@nereolopez Ah, now it finally is the best auth experience available. Hopefully they fix the auth-helpers repo by including this new update.

I'm not actually a repo contributor my dude - so I can't close the issue - but, hey, good that it works though!

@mousindev
Copy link
Author

Sorry @ARMATAV, I am in the middle of my first paternity leave and went straight to test it. As you left the note I did not check and just assumed you were. I am closing the issue then. Thanks!

@ARMATAV
Copy link

ARMATAV commented Aug 23, 2022

@nereolopez Congratulations on the kid!

@egor-romanov egor-romanov added the p3 Priority 3 label Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth bug Something isn't working p3 Priority 3
Projects
None yet
Development

No branches or pull requests

8 participants