Comments (8)
Thanks for all the detailed info, @phamhieu! It'll be helpful for other people too.
You can take a look as reference https://github.com/phamhieu/supabase-dart-demo.
I didn't know of the demo app. I'll look into it.
The default value for token expiry is 3600 seconds (1 hour). If you don't use the app for 1 hour, it will expire.
there's a bug with gotrue-dart library which causes wrong expiry calculation check.
It must be the bug you fixed yesterday, right? I left my app (with supabase 0.0.1-dev.20
) untouched for longer than 1 hour, and there was no token expiry error this time. It is great that we don't have to care about it!
you can listen for resume event with WidgetsBindingObserver and trigger recoverSession
If the expiry-related issue was fixed, I wonder if you still need to listen for the resume event. The token does not expire while you keep using the app, so recovery is only needed when the app is launched, but AFAIK the state does not change to resume
immediately after an app launch. I'll try it anyway and other different ways as well, and choose the best one for me.
Now that you clarified that the behaviour I thought was a bug was actually an expected behaviour, you can close this issue once documentation is improved. Thank you!
from gotrue-dart.
If the expiry-related issue was fixed, I wonder if you still need to listen for the resume event. The token does not expire while you keep using the app, so recovery is only needed when the app is launched
the token will not expire if app runs on foreground. Listening for the resume event will help recover session when app resumes from background state. I have an example here
from gotrue-dart.
@kaboc, there is an issue with session recovery timer. I fixed it on the latest version supabase: ^0.0.1-dev.20
let me know if you still facing the issue.
from gotrue-dart.
@phamhieu
I updated the package to 0.0.0-dev.20
, ran the app, and confirmed that the callback of onAuthStateChange
was called.
However, it wasn't called when recoverSession()
was executed after I hot-restarted the app. Since then it has never been called, regardless of whether a hot restart or a full restart.
Is the session recovery timer you mentioned related to expiration of a token/session? I think my last sign-in to the app was yesterday or the day before, so it is likely that the session had already expired at the point of my first attempt today. I hope this gives you a clue.
from gotrue-dart.
recoverSession will only trigger an event if the current session is invalid and it refresh to a new one
In your case, can check your access token expiry to confirm? Can you also test on a fresh install to ensure that the persistent session value is right.
The timer bug is because of invalid expiryAt persistent session value.
P.S if you want to force a refresh, then you should call auth.api.refreshAccessToken instead
from gotrue-dart.
@phamhieu
Thank you for the explanation. What I understood is, the callback is only called when the session is refreshed, and just recovering a session before expiry does not trigger it.
I think it is reasonable, but onAuthStateChenge()
has no document and the method name does not mention the timing. As a user, I guess from the name that its callback function is called whenever the auth state changes, including the transition from the signed-out state right after an app launch to the recovered signed-in session after recoverSession()
.
What is more confusing is that it is triggered even on recovery if the access token has already expired.
- on sign in: triggered
- on recovery before expiry: not triggered
- on recovery after expiry: triggered
I think this could be improved for better usability.
I actually expected that I could write only once inside the callback the code for navigating a user after sign-in and recovery, but I'll write it separately as a workaround if it's not possible.
from gotrue-dart.
P.S if you want to force a refresh, then you should call auth.api.refreshAccessToken instead
It is also what I'm not sure about. It looks like the token expires if I don't use an app for a while after launching it. I notice the expiry only when a query to the database fails.
- Why does it expire although
autoRefreshToken
istrue
by default? - Should expiry be checked before every fetch?
- Or do I have to see the failure first and then try
refreshAccessToken()
? - Is there a method for the checking?
I would be grateful if you'd direct me to a related document/thread/issue.
from gotrue-dart.
but onAuthStateChenge() has no document and the method name does not mention the timing
Totally agree with you. We need to improve the document.
I actually expected that I could write only once inside the callback the code for navigating a user after sign-in and recovery, but I'll write it separately as a workaround if it's not possible.
i just update flutter demo app for Supabase. You can take a look as reference https://github.com/phamhieu/supabase-dart-demo.
- It uses SplashScreen to check persistent session and recover on app open.
- It uses a singleton to manage supabase-client and persist session .
- We should have only 1 instance of supabase-client per app.
It looks like the token expires if I don't use an app for a while after launching it.
The default value for token expiry is 3600 seconds (1 hour). If you don't use the app for 1 hour, it will expire. You can update this value if you want (under Authentication settings).
There are many ways to solve this. Exp: you can listen for resume event with WidgetsBindingObserver
and trigger recoverSession
Why does it expire although autoRefreshToken is true by default?
there's a bug with gotrue-dart library which causes wrong expiry calculation check.
Should expiry be checked before every fetch?
no, you don't need. gotrue-dart has a timer to auto refresh the token. here
I hope this helps, let me know if you need any other clarification.
from gotrue-dart.
Related Issues (20)
- Regexp with positive lookbehind are not fully supported by all browser
- Token is returned as URI fragment with a `#`, but is expected as query parameters HOT 4
- Update signUp to use named parameters HOT 1
- Need more information about oAuth2 HOT 8
- Change Provider enum to ProviderEnum in order to avoid name conflicts with riverpod HOT 2
- Apple sign in support? HOT 2
- Delete User Account HOT 4
- Support native auth on mobile devices HOT 19
- "Current session is missing data" when restoring a phone user HOT 10
- Missing documentation in comparison to javascript HOT 3
- Implement setSession API from gotrue-js client
- Missing keys in User.toJson()
- currentSession.user is not updated in GoTrueClient.update()
- Release new version because gotrue:0.1.2 is not exporting gotrue_error.dart class HOT 1
- Wrong `Authorization` header is used when refreshing tokens HOT 1
- SignUp does not return Session/User object HOT 1
- type `Null` is not a subtype of type `Map<String, dynamic>` in type cast HOT 1
- No persist storage provided HOT 1
- Persistant user session HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gotrue-dart.