supabase-community / auth-ui

Pre-built Auth UI for React
https://supabase.com/docs/guides/auth/auth-helpers/auth-ui
MIT License
487 stars 121 forks source link

Redirection not working #44

Closed user72356 closed 1 year ago

user72356 commented 1 year ago

Using the latest version of @supabase/auth-ui-react (0.2.2) and @supabase/supabase-js (2.0.0)

In a React app (NextJS), using e-mail/password authentication, the redirectTo prop doesn't appear to do anything. No redirection takes place after login, although the login is successful.

This is how I'm using the component within a page:

export default function LoginPage()
{
    return <Auth supabaseClient={ supabase } redirectTo={ "http://localhost:3000/" } appearance={ { theme: ThemeSupa } } />;
}

The Supabase console has http://localhost:3000/ in the list of redirects.

I've tried with and without the forward slash.

eeston commented 1 year ago

Try listening to supabase.auth.onAuthStateChange() and redirect based on events/session chnages there.

user72356 commented 1 year ago

Try listening to supabase.auth.onAuthStateChange() and redirect based on events/session chnages there.

I ended up using Auth.useUser() and if the user exists then I do a router.replace("/"). Do you see anything wrong with this approach?

  const { user } = Auth.useUser();
  const router = useRouter();

  useEffect(() => 
  {
      if (user)
      {
          void router.replace("/");
      }
  }, [user, router]);

  return <Auth supabaseClient={ supabase } appearance={ { theme: ThemeSupa } } />;
eeston commented 1 year ago

I forgot auth-ui actually uses supabase.auth.onAuthStateChange() under the hood, so your approach should work fine.