0

I have an Express server that right now signs into Supabase. The authentication is successful as when I print data it gives me the user and session information. But when I try to retrieve the user or session information, it's just null. In the below code, event becomes INITIAL_SESSION, session becomes null, and user also becomes null. I also don't really understand how Supabase's sessions work. I would gladly appreciate if anyone could shed some light on this.

app.post("/login", async(req, res) => {
    const {data, error} = await supabase.auth.signInWithPassword({
        email: req.body.email,
        password: req.body.password
    })
    await supabase.auth.onAuthStateChange((event, session) => {
        console.log(event, session);
    })
    if (error) {
        console.log(error);
        res.status(401).send("Email/password combination is wrong.");
    }
    else {
        const { data: { user } } = await supabase.auth.getUser();
        console.log(user);
        res.status(202).send("Login successful.");
    }
})
Ansh
  • 1
  • 1
    You shouldn't be requesting `getUser` in this code since you already have the `session` sent back in the data object on sign in which means you are signed in. There are missing parts to this code as it doesn't show now you are initiating the supabase client. The client initiation behaves differently when in a server environment compare to a client environment. – Andrew Smith Jun 19 '23 at 23:33

1 Answers1

0

Supabase enables Email confirmation by default before signing users in. The way I fixed it was by going into Authentication > Providers > Email and unselecting "Confirm email".

LW001
  • 2,452
  • 6
  • 27
  • 36