Get supabase `user` server side in next.js
00:05 11 Jan 2022

I am attempting to get the current logged in supabase user while server side.

I have attempted to use const user = supabase.auth.user(); but I always get a null response.

I have also attempted const user = supabase.auth.getUserByCookie(req) but it also returns null. I think because I am not sending a cookie to the api when calling it from the hook.

I have tried passing the user.id from the hook to the api but the api is not receiving the parameters.

I also attempted this approach but the token is never fetched. It seems to not exist in req.cookies.

    let supabase = createClient(supabaseUrl, supabaseKey);
    let token = req.cookies['sb:token'];
    if (!token) {
        return 
    }
    let authRequestResult = await fetch(`${supabaseUrl}/auth/v1/user`, {
        headers: {
            'Authorization': `Bearer ${token}`,
            'APIKey': supabaseKey
        }
    });
`

Does anyone know how to get the current logged in user in server side code?

next.js supabase supabase-database