S
Supabase2y ago
Java

getSession() is null after login?

I'm currently trying to get the user in a client component however:
const session = supabaseClient.auth.getSession().then((data) => {
console.log(data);
});
const session = supabaseClient.auth.getSession().then((data) => {
console.log(data);
});
returns
{data: {…}, error: null}
data: {session: null}
{data: {…}, error: null}
data: {session: null}
All of my other widgets that use the server client is able to get user no problem but for some reason session must not be persisted locally? When users login from /login it's from a server component.
8 Replies
j4
j42y ago
Can you verify a session exists in the browser? Would be a cookie if you're using auth helpers.
Java
JavaOP2y ago
@j4 I have a cookie for auth (localhost):
```%5B%22eyJhbGciOiJIUzI1NiIsImtpZCI6IkFqaU11Sm9GMStmNHMxdHoiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwiZXhwIjoxNzAyOTAxNzUzLCJpYXQiOjE3MDIyOTY5NTMsImlzcyI6Imh0dHBzOi8vdGlraHltY2hxbnB2dWdyc29sanYuc3VwYWJhc2UuY28vYXV0aC92MSIsInN1YiI6ImM3MmZhNDQwLWFlMzUtNDkwNy1hNTU5LTllYzkwNmQ2NGQ3YiIsImVtYWlsIjoiY2hyaXN0b3BoZXJmb3JyZXN0OTJAZ21haWwuY29tIiwicGhvbmUiOiIiLCJhcHBfbWV0YWRhdGEiOnsicHJvdmlkZXIiOiJlbWFpbCIsInByb3ZpZGVycyI6WyJlbWFpbCJdfSwidXNlcl9tZXRhZGF0YSI6eyJhdmF0YXJfdXJsIjoiIiwiZnVsbF9uYW1lIjoiY2hyaXN0b3BoZXIgZm9ycmVzdCJ9LCJyb2xlIjoiYXV0aGVudGljYXRlZCIsImFhbCI6ImFhbDEiLCJhbXIiOlt7Im1ldGhvZCI6InBhc3N3b3JkIiwidGltZXN0YW1wIjoxNzAyMjk2OTUzfV0sInNlc3Npb25faWQiOiJkMGY2ZWM3My0yY2Q1LTQyMjMtYTc2MC03MzMwNTEwNGNiOTMifQ.lPk8SAC61bxQ_hHCIlYIq62ATLuas74e4SFYbjm36Hk%22%2C%22JXCGlFrKoMdGOdQSpj8IkQ%22%2Cnull%2Cnull%2Cnull%5D
```%5B%22eyJhbGciOiJIUzI1NiIsImtpZCI6IkFqaU11Sm9GMStmNHMxdHoiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwiZXhwIjoxNzAyOTAxNzUzLCJpYXQiOjE3MDIyOTY5NTMsImlzcyI6Imh0dHBzOi8vdGlraHltY2hxbnB2dWdyc29sanYuc3VwYWJhc2UuY28vYXV0aC92MSIsInN1YiI6ImM3MmZhNDQwLWFlMzUtNDkwNy1hNTU5LTllYzkwNmQ2NGQ3YiIsImVtYWlsIjoiY2hyaXN0b3BoZXJmb3JyZXN0OTJAZ21haWwuY29tIiwicGhvbmUiOiIiLCJhcHBfbWV0YWRhdGEiOnsicHJvdmlkZXIiOiJlbWFpbCIsInByb3ZpZGVycyI6WyJlbWFpbCJdfSwidXNlcl9tZXRhZGF0YSI6eyJhdmF0YXJfdXJsIjoiIiwiZnVsbF9uYW1lIjoiY2hyaXN0b3BoZXIgZm9ycmVzdCJ9LCJyb2xlIjoiYXV0aGVudGljYXRlZCIsImFhbCI6ImFhbDEiLCJhbXIiOlt7Im1ldGhvZCI6InBhc3N3b3JkIiwidGltZXN0YW1wIjoxNzAyMjk2OTUzfV0sInNlc3Npb25faWQiOiJkMGY2ZWM3My0yY2Q1LTQyMjMtYTc2MC03MzMwNTEwNGNiOTMifQ.lPk8SAC61bxQ_hHCIlYIq62ATLuas74e4SFYbjm36Hk%22%2C%22JXCGlFrKoMdGOdQSpj8IkQ%22%2Cnull%2Cnull%2Cnull%5D
j4
j42y ago
Interesting. What version of auth helpers?
Java
JavaOP2y ago
"@supabase/auth-helpers-nextjs": "latest"
"@supabase/auth-helpers-nextjs": "latest"
j4
j42y ago
What's the code you're using to create that client component?
Java
JavaOP2y ago

import { supabaseClient } from "@/lib/supabase-client";
...
const session = supabaseClient.auth.getSession().then((data) => {
console.log(data);
});

import { supabaseClient } from "@/lib/supabase-client";
...
const session = supabaseClient.auth.getSession().then((data) => {
console.log(data);
});
import { createClient } from "@supabase/supabase-js";
...
export const supabaseClient = createClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL || "",
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || ""
);
import { createClient } from "@supabase/supabase-js";
...
export const supabaseClient = createClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL || "",
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || ""
);
j4
j42y ago
Ok, so there's your issue. If you're going to use auth helpers, you need to use their methods of clients or creating clients. By using the native createClient() function, that defaults to looking at browser localStorage for storage; whereas the auth helpers use cookies for storage.
Java
JavaOP2y ago
Ok im dumb i should've obviously used:
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";
and its corresponding auth methods 🥹

Did you find this page helpful?