Giter VIP home page Giter VIP logo

Comments (10)

kiwicopple avatar kiwicopple commented on August 15, 2024 1

I think this is a bug with gotrue-js. I'll migrate the issue there.

from gotrue-js.

dthyresson avatar dthyresson commented on August 15, 2024 1

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#L310

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.

dthyresson avatar dthyresson commented on August 15, 2024 1

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#L310

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

Hi @snax4a there is a timer that will refresh the token. See

https://github.com/supabase/gotrue-js/blob/d7b334a4283027c65814aa81715ffead262f0bfa/src/GoTrueClient.ts#L680

If autoRefreshToken is true (which should be the default) then when

https://github.com/supabase/gotrue-js/blob/d7b334a4283027c65814aa81715ffead262f0bfa/src/GoTrueClient.ts#L645

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.

dthyresson avatar dthyresson commented on August 15, 2024 1

But glad it's working for you. All best.

from gotrue-js.

github-actions avatar github-actions commented on August 15, 2024

🎉 This issue has been resolved in version 1.9.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

from gotrue-js.

guido4000 avatar guido4000 commented on August 15, 2024

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.

thorwebdev avatar thorwebdev commented on August 15, 2024

@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.

guido4000 avatar guido4000 commented on August 15, 2024

@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.

snax4a avatar snax4a commented on August 15, 2024

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.

snax4a avatar snax4a commented on August 15, 2024

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#L310

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.