Skip to content
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

User not connected when clicking on change password link #25987

Closed
ek-ma opened this issue May 10, 2024 · 3 comments
Closed

User not connected when clicking on change password link #25987

ek-ma opened this issue May 10, 2024 · 3 comments
Labels
auth All thing Supabase Auth related

Comments

@ek-ma
Copy link

ek-ma commented May 10, 2024

Bug report

  • [ x ] I confirm this is a bug with Supabase, not with my own application.
  • [ x ] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

I am using Nuxt 3.

I am encountering an issue when implementing the "reset password" flow that can be described in the supabase documentation here.

The first part about sending the reset password email works fine.
The issue happens when the user clicks on the link and is redirected to the change password page : he is redirected to the login page.
As described in the documentation, the "change password" page is protected to only allow authenticated users. Thus, I added a middleware. However, It seems that the user is not yet connected when redirected to this page and is then redirected to the login page.

Here is my middleware :

export default defineNuxtRouteMiddleware(async (to, from) => {   
    const supabase = useSupabaseClient()  
    const { data: { user } } = await supabase.auth.getUser()  
    console.log(user)
    if (!user) {  
        const localePath = useLocalePath()  
        return navigateTo(localePath('login'))  
    }  
})

==> If I try to log the user using a console.log, the output is null

Here is my supabaseClient :

import { createClient } from "@supabase/supabase-js";

const supabaseUrl = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_KEY;

export const supabase = createClient(supabaseUrl, supabaseKey);

I tried every thing I saw online but nothing worked. I tried several thing : use the supabase.auth.exchangeCodeForSession(code) method, but that produces a both auth code and code verifier should be non-empty error message.

Expected result

  1. I click on the reset password button
    image

  2. I receive the mail & click on it
    image

  3. I should be logged in and arrive on the change password page
    image

System information

  • OS: [ Windows 11 ]
  • Browser : Google Chrome
  • Version of supabase-js: [e.g. 6.0.2]
  • Version of Node.js: 20.12.2I am

Additional context

I am using Nuxt 3

@ek-ma ek-ma added the bug Something isn't working label May 10, 2024
@SergejSi
Copy link

I am experiencing the same issue, but I am using Next.js 14.2.3. The behavior is identical: the user gets redirected to the login page instead of the change password page after clicking the reset password link. Any solutions or workarounds would be greatly appreciated.

@ek-ma
Copy link
Author

ek-ma commented May 23, 2024

Apparently, Nuxt's middleware is ran 2 times : first during the server side rendering and a second time during the hydration process (client side)
By logging, I noticed that the user is not connected in the server side middleware but it is in the client side.

As a workaround, I added a return statement in my middleware when running server side like so :

export default defineNuxtRouteMiddleware(async () => {
    if (import.meta.server) return

    const user = useSupabaseUser()
    if (!user.value) {
        const localePath = useLocalePath()
        return await navigateTo(localePath('login'))
    }
})

I believe you should be able to do the same thing using Next.js

But that is not ideal : you would prefer to be connected server side, since this workaround allows the user to have a glimpse at the page during its loading time but i have no idea how & no answer to this issue so far.

@encima encima added auth All thing Supabase Auth related and removed bug Something isn't working labels May 29, 2024
@encima
Copy link
Contributor

encima commented May 29, 2024

As @ek-ma said, this is expected behaviour of Nuxt, although not ideal behaviour this is the recommended implementation from Nuxt.

I would close this for now as it is Framework specific behaviour rather than a bug with Supabase.

If you do want to add it to the docs, feel free to open a PR and we can reopen this while the PR is in review!

@encima encima closed this as completed May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth All thing Supabase Auth related
Projects
None yet
Development

No branches or pull requests

3 participants