-
-
Notifications
You must be signed in to change notification settings - Fork 167
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
feat: added async getSession/getUser method #285
Conversation
} | ||
|
||
const hasExpired = this.currentSession.expires_at | ||
? this.currentSession.expires_at <= Date.now() / 1000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have some threshold here? we already have a constate for this i think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it needed here? If this handler works for only refreshing the token if it's already somehow expired, and the regular refresh timer handles refreshing tokens early, users rarely have to wait for the refresh request before their normal request.
Not opposed to it, just wondering what the benefits are vs the drawbacks?
@alaister instead of // SupabaseClient constructor
this.rest = new PostgrestClient(url, {
...,
fetch: async (input, init) => {
const headers = init?.headers ?? {}
const accessToken = await getAccessToken()
if (accessToken) {
headers['Authorization'] = `Bearer ${accessToken}`
}
return this.fetch(input, { ...init, headers })
},
}) |
@soedirgo That works too! Did you see this: supabase/storage-js#67? It essentially makes all of the headers async. What are your thoughts on the benefits/drawbacks of all three approaches? |
Ah yup, that works as well. I think the only real drawback of Meanwhile the |
I agree that a custom fetch is the most flexible, but you're right does feel a bit dirty. Personally, I could go either way between the custom @inian would love your thoughts here too. |
🎉 This PR is included in version 1.23.0-next.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
🎉 This PR is included in version 2.0.0-rc.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
What kind of change does this PR introduce?
This is a proof of concept for a new async
getSession()
method. This allows users to get a valid session (not expired) as opposed to the synchronoussession()
method, which may return an expired session.Additional context
I know we're planning to discuss this further, but since I already had most of the code, I thought I'd throw something together quickly. It will hopefully make our discussion clearer if we have something to look at!
Once
gotrue-js
has a method like this (or similar) it will allowsupabase-js
to work something like:Then whenever
postgrestClient
is about to send a query, it can call thegetAccessToken()
method, which will return an up-to-date token.Note: While this isn't a breaking change in
gotrue-js
(purely additive), it will be a breaking change once we start doing the above insupabase-js
, as some users may rely on the first request possible sending an expired token. We may consider ignoring this use case though, as it is a strange one!Resolves #143 and #23
Supersedes #265 and #147