supabase / auth-js

An isomorphic Javascript library for Supabase Auth.
MIT License
366 stars 164 forks source link

onAuthStateChange when using signup email link in React #615

Open maxhvlm opened 2 years ago

maxhvlm commented 2 years ago

Bug report

Describe the bug

When signing up using email, the user successfully gets redirected to the redirectTo link specified with the correct access and refresh tokens. They get picked up by supabase and the session gets saved to the localstorage. However no onAuthStateChangeevent is fired, only after refreshing the page the session gets parsed from the local storage and the event is fired.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Add event listener to app.js: useEffect(() => { supabase.auth.onAuthStateChange((_event, session) => { console.log("Got session form onAuthStateChange", session, _event); }); }, []); 2.. Call supabase.auth.signup to generate the email supabase.auth.signUp({ email, password }, { redirectTo: "{our_url}/login"})
  2. Click on the email link

Expected behavior

Once supabase obtains a valid session and updates it in the localstorage, the onAuthStateChange should get fired.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

Additional context

We're trying to redirect the user back to the login screen after successful signup as we have a hook that checks if a session already exists and redirect users either to the appropriate homepage (profile signup, dashboard, ...).

However this functionality isn't working as no session is set after accessing the page from the email signup link.

j4w8n commented 2 years ago

Looks like the same issue as https://github.com/supabase/gotrue-js/issues/518. Be sure to checkout the last two comments.

kangmingtay commented 1 year ago

Hey @maxhvlm, I've tried to reproduce this on the react example in gotrue-js by moving the onAuthStateChange method into a useEffect block as you described (https://github.com/supabase/gotrue-js/blob/a831a356dbef665d8d6e3285c96346494628dd72/example/react/src/App.js#L24-L27) but to no avail. Do you happen to have a repo that I can check out to reproduce this error?