Comments (10)
I think this is a bug with gotrue-js. I'll migrate the issue there.
from gotrue-js.
I can't find any info in docs about how can i manually refresh tokens in supabase auth
Hi @snax4a while I don't see this method documented on the Supabase API reference, perhaps the refreshSession()
might work for you?
You can see a test here:
test('refreshSession() forces refreshes the session to get a new refresh token for same user', async () => {
const { email, password } = mockUserCredentials()
const { error: initialError, session } = await authWithSession.signUp({
email,
password,
})
expect(initialError).toBeNull()
expect(session).not.toBeNull()
const refreshToken = session?.refresh_token
const { error, user, data } = await test('refreshSession() forces refreshes the session to get a new refresh token for same user', async () => {
const { email, password } = mockUserCredentials()
const { error: initialError, session } = await authWithSession.signUp({
email,
password,
})
expect(initialError).toBeNull()
expect(session).not.toBeNull()
const refreshToken = session?.refresh_token
const { error, user, data } = await authWithSession.refreshSession()
expect(error).toBeNull()
expect(user).not.toBeNull()
expect(data).not.toBeNull()
expect(user?.email).toEqual(email)
expect(data).toHaveProperty('refresh_token')
expect(refreshToken).not.toEqual(data?.refresh_token)
})()
expect(error).toBeNull()
expect(user).not.toBeNull()
expect(data).not.toBeNull()
expect(user?.email).toEqual(email)
expect(data).toHaveProperty('refresh_token')
expect(refreshToken).not.toEqual(data?.refresh_token)
})
Demo that refreshSession()
will create new refresh token on the user's session.
Could this work for you?
from gotrue-js.
I can't find any info in docs about how can i manually refresh tokens in supabase auth
Hi @snax4a while I don't see this method documented on the Supabase API reference, perhaps the
refreshSession()
might work for you?
See:
https://github.com/supabase/gotrue-js/blob/d7b334a4283027c65814aa81715ffead262f0bfa/src/GoTrueClient.ts#L310You can see a test here:
test('refreshSession() forces refreshes the session to get a new refresh token for same user', async () => { const { email, password } = mockUserCredentials() const { error: initialError, session } = await authWithSession.signUp({ email, password, }) expect(initialError).toBeNull() expect(session).not.toBeNull() const refreshToken = session?.refresh_token const { error, user, data } = await test('refreshSession() forces refreshes the session to get a new refresh token for same user', async () => { const { email, password } = mockUserCredentials() const { error: initialError, session } = await authWithSession.signUp({ email, password, }) expect(initialError).toBeNull() expect(session).not.toBeNull() const refreshToken = session?.refresh_token const { error, user, data } = await authWithSession.refreshSession() expect(error).toBeNull() expect(user).not.toBeNull() expect(data).not.toBeNull() expect(user?.email).toEqual(email) expect(data).toHaveProperty('refresh_token') expect(refreshToken).not.toEqual(data?.refresh_token) })() expect(error).toBeNull() expect(user).not.toBeNull() expect(data).not.toBeNull() expect(user?.email).toEqual(email) expect(data).toHaveProperty('refresh_token') expect(refreshToken).not.toEqual(data?.refresh_token) })Demo that
refreshSession()
will create new refresh token on the user's session.
Could this work for you?Thanks for your reply, I can successfully refresh session using
supabase.auth.refreshSession()
In every supabase query I'm doing the following:if (error) { if (error.message?.includes('JWT expired')) { await supabase.auth.refreshSession() } throw new Error(error.message) }However, I wonder why supabase-js library is not refreshing it automatically
Hi @snax4a there is a timer that will refresh the token. See
If autoRefreshToken
is true (which should be the default) then when
if called a timer starts and should refresh before the session expires -- as long as there is an expires.
But from what I can see setSession
would start that timer.
from gotrue-js.
But glad it's working for you. All best.
from gotrue-js.
🎉 This issue has been resolved in version 1.9.2 🎉
The release is available on:
Your semantic-release bot 📦🚀
from gotrue-js.
This solved the problem half-way for me. After one hour I still get a problem accessing an API route as the auth call in the API route await supabase.auth.api.getUser(token);
does fail. Only after reloading the app a token refresh is being triggered.
from gotrue-js.
@guido4000 this is something I'm working on as part of #33. Also, I'd love to get your feedback on the proposed SSR experience there.
from gotrue-js.
@thorwebdev
The API looks great.
For the Next.js SSR example it would nice to see the use of SWR in the pages/profile.js route similar to pages/index.js.
https://github.com/vercel/swr#ssr-with-nextjs
from gotrue-js.
I'm also experiencing this issue, after one hour when I call:
await supabase.auth.api.getUser(session.access_token)
In my UserContext i am subscribing to onAuthStateChange event so when the token is refreshed i should be getting fresh session object.
useEffect(() => {
const session = supabase.auth.session()
setSession(session)
setUser(session?.user ?? null)
setSessionLoading(false)
const { data: authListener } = supabase.auth.onAuthStateChange(
async (event, session) => {
setSession(session)
setUser(session?.user ?? null)
}
)
return () => {
authListener?.unsubscribe()
}
}, [])
I can't find any info in docs about how can i manually refresh tokens in supabase auth
from gotrue-js.
I can't find any info in docs about how can i manually refresh tokens in supabase auth
Hi @snax4a while I don't see this method documented on the Supabase API reference, perhaps the
refreshSession()
might work for you?See:
You can see a test here:
test('refreshSession() forces refreshes the session to get a new refresh token for same user', async () => { const { email, password } = mockUserCredentials() const { error: initialError, session } = await authWithSession.signUp({ email, password, }) expect(initialError).toBeNull() expect(session).not.toBeNull() const refreshToken = session?.refresh_token const { error, user, data } = await test('refreshSession() forces refreshes the session to get a new refresh token for same user', async () => { const { email, password } = mockUserCredentials() const { error: initialError, session } = await authWithSession.signUp({ email, password, }) expect(initialError).toBeNull() expect(session).not.toBeNull() const refreshToken = session?.refresh_token const { error, user, data } = await authWithSession.refreshSession() expect(error).toBeNull() expect(user).not.toBeNull() expect(data).not.toBeNull() expect(user?.email).toEqual(email) expect(data).toHaveProperty('refresh_token') expect(refreshToken).not.toEqual(data?.refresh_token) })() expect(error).toBeNull() expect(user).not.toBeNull() expect(data).not.toBeNull() expect(user?.email).toEqual(email) expect(data).toHaveProperty('refresh_token') expect(refreshToken).not.toEqual(data?.refresh_token) })Demo that
refreshSession()
will create new refresh token on the user's session.Could this work for you?
Thanks for your reply, I can successfully refresh session using supabase.auth.refreshSession()
In every supabase query I'm doing the following:
if (error) {
if (error.message?.includes('JWT expired')) {
await supabase.auth.refreshSession()
}
throw new Error(error.message)
}
However, I wonder why supabase-js library is not refreshing it automatically
from gotrue-js.
Related Issues (20)
- "Test Phone Numbers and OTPs" does not work with Twilio HOT 3
- SSO auth not working HOT 10
- Click on Magic Link triggers spam link protection in uBlock Origin Ad Blocker HOT 2
- Admin create user will let anyone get the account HOT 2
- Twitter and social auth redirectUrl is unreliable HOT 1
- Unable to change email, OTP verification fails. "User not found" HOT 12
- Error when Verifying OTP for Email Change - User not found (Supabase) HOT 1
- No way to display cusom error messages HOT 8
- Request for Improved Response Status in supabase.auth HOT 4
- Sending Data during Google Login and Retrieving it After Authentication on Redirected URL HOT 2
- Unable to refresh provider_token HOT 5
- signInWithPassword ignores options.data instead of writing it to user_metadata HOT 8
- auth.identities type cast issue HOT 10
- Angular application started polluting logs with exception. HOT 13
- Using verbatimModuleSyntax in tsconfig.json causes type check failure HOT 2
- Retrieving session from local storage is not working HOT 1
- Supabase password requirement breaks signUp through `signInWithOtp` function HOT 1
- { "code": 500, "message": "An error has occurred: fetch failed", "requestId": "ca5520d8-5cc6-40e3-9f27-ce1ae6227025" } HOT 1
- getSession user data out of data with access_token
- Functions that call `_removeSession` internally do not trigger `SIGNED_OUT` event when the function fails HOT 6
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-js.