Issues with Login Conditional Using Supabase and React

I'm encountering an issue with a login component in a React project using Supabase for authentication. The login flow seems to function, but I'm having trouble with a specific conditional that's supposed to redirect the user to the dashboard after successful login.

Here's the relevant code snippet:

const { supabase, session } = useContext(SupabaseContext)


const handleLogin = () => {
  supabase.auth
    .signInWithOAuth({ provider: 'google' })
    .then(({ data, error }) => {
      console.log(data, error);
      if (data && session && session.access_token) {
        navigate('/dashboard');
      } else {
        console.log('Login failed, no user data.');
      }
    })
    .catch((err) => {
      console.error('Error during login:', err);
    });
};


The issue arises with the conditional if (data && session && session.access_token). After "successfully" logging in with Google, I am unexpectedly redirected first to the '/dashboard' and then immediately to the home page, instead of staying on the dashboard. This behavior seems inconsistent with the intended functionality of the conditional statement.

I set up a redirect in Supabase but it didn't work.
image.png
Was this page helpful?