Unexpected Supabase Auth Session Logout After 2–3 Seconds (email and OAuth)

I’m seeing a new issue in my React + Vite app where Supabase auth (email/password and OAuth) signs users in successfully but then automatically signs them out 2–3 seconds later—no error is shown. Steps to Reproduce 1. User signs in (email/password or OAuth). 2. Supabase returns a valid session and access token. 3. After ~2–3 seconds, an SIGNED_OUT event fires and the session is cleared. Expected Users stay signed in until they explicitly sign out or the JWT expires. Actual The session is removed almost immediately after login. --- Environment * @supabase/supabase-js ^2.49.4 * vite ^5.4.1 * React + Vite * Node.js (current LTS) * Browsers: Chrome / Firefox / Edge Auth Client Initialization
export const supabase = createClient<Database>(
import.meta.env.VITE_SUPABASE_URL,
import.meta.env.VITE_SUPABASE_ANON_KEY,
{
auth: {
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true,
},
}
);
export const supabase = createClient<Database>(
import.meta.env.VITE_SUPABASE_URL,
import.meta.env.VITE_SUPABASE_ANON_KEY,
{
auth: {
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true,
},
}
);
Key Hooks
useEffect(() => {
// handle SIGNED_IN, TOKEN_REFRESHED, SIGNED_OUT, INITIAL_SESSION events
const { data: { subscription }} = supabase.auth.onAuthStateChange(
(event, sessionData) => { … }
);
// initial session fetch
const { data: { session: initialSession }} = await supabase.auth.getSession();
// setSession, setUser, setInitialized…
}, [queryClient, isFirstTimeUser, handleWelcomeEmail]);
useEffect(() => {
// handle SIGNED_IN, TOKEN_REFRESHED, SIGNED_OUT, INITIAL_SESSION events
const { data: { subscription }} = supabase.auth.onAuthStateChange(
(event, sessionData) => { … }
);
// initial session fetch
const { data: { session: initialSession }} = await supabase.auth.getSession();
// setSession, setUser, setInitialized…
}, [queryClient, isFirstTimeUser, handleWelcomeEmail]);
--- What I’ve Tried * Confirmed persistSession: true. * No errors in console/network. * Env vars look correct. * No stray sign-out logic elsewhere. Questions 1. Are there known issues or config tweaks that could force immediate sign-outs? 2. Could cookie, CORS or SameSite policies be wiping the session? 3. Any extra logging or debug flags in @supabase/supabase-js to trace why SIGNED_OUT is triggered? Any pointers or debugging tips appreciated—thanks!
26 Replies
Wilson Ibekason
Wilson IbekasonOP2mo ago
// Check for first-time user and handle welcome email useEffect(() => { if (!session || !initialized) return; const checkFirstTimeUser = async () => { try { const profileData = await fetchProfile(session.user.id); const isFirstTime = isFirstTimeUser("SIGNED_IN", session, profileData); if (isFirstTime) { setIsFirstLogin(true); await handleWelcomeEmail(session, profileData); } else { setIsFirstLogin(false); } } catch (error) { console.error( "[useSupabaseAuth] Error checking first time user:", error ); setIsFirstLogin(false); } }; // Only check on new sessions, not on every render if (session && !isFirstLogin) { checkFirstTimeUser(); } }, [session?.user.id, initialized, isFirstTimeUser, handleWelcomeEmail]); const signInWithGoogle = async () => { const { data, error } = await supabase.auth.signInWithOAuth({ provider: "google", options: { redirectTo: window.location.origin, }, }); if (error) { console.error("[useSupabaseAuth] Google sign-in error:", error); } return { data, error }; };
j4
j42mo ago
Any chance the signOut button is getting silently clicked by React, or prefetched if it's a link?
Wilson Ibekason
Wilson IbekasonOP2mo ago
I've cross checked the codebase for any randoms calls for await supabase.auth.signOut(); and discovered that it's only on funcs that're manually called like signout btn, etc.
GlassHouse
GlassHouse2mo ago
I see similar behaviour every once and a while in one of my supabase auth apps as well. Can't seem to pinpoint why. Does Supabase do any type of checks on a users account? Don't know too much about the Supabase at this point to make an informed comment. But would love to dig deeper to see if it's something that might be in my control to fix
Wilson Ibekason
Wilson IbekasonOP2mo ago
Pls, they do need to look into this, I don't understand how my app auth suddenly started logging my users out after couple of secs. The code was working perfectly well and then just suddenly started this behavior, I've checked the codebase and still can't really understand the root of the problem, what should I do cause my entire app is contolled with supabase auth??? Thanks @GlassHouse
j4
j42mo ago
Regarding debugging: you can set the auth: { debug: true } option
silentworks
silentworks2mo ago
What code is inside of your supabase.auth.onAuthStateChange? sharing partials and leaving out some detail isn't very helpful when asking for help. It's also better to have a reproducible example repo.
Wilson Ibekason
Wilson IbekasonOP2mo ago
I should have done that 🙏 Let me drop more details
Wilson Ibekason
Wilson IbekasonOP2mo ago
Gist
Auth.tsx
GitHub Gist: instantly share code, notes, and snippets.
Wilson Ibekason
Wilson IbekasonOP2mo ago
These covers all the details
silentworks
silentworks2mo ago
@Wilson Ibekason can you please stop posting the same thing in multiple channels. If you do so again your account will go into timeout as it's considering spamming.
Wilson Ibekason
Wilson IbekasonOP2mo ago
@silentworks I understand and won't post again, but I would appreciate any response, I've provided more context. The problem is affecting not just me but my users
silentworks
silentworks2mo ago
You aren't guaranteed a response in any set timeframe on here. It's a user helping user platform.
Wilson Ibekason
Wilson IbekasonOP2mo ago
It's OK, just wanted to know if it's a supabase issue, it's been quite a long I utilized supabase Auth.
silentworks
silentworks2mo ago
How are you verifying that your user is signed in? have you checked that there is an entry in your browser's localStorage? It won't be as we would have had more users on here running into the issue if it was a Supabase Auth issue. It's more likely an application implementation issue.
Wilson Ibekason
Wilson IbekasonOP2mo ago
It's doesn't use localstorage but cookies, it gets cleared out after couple of secs
silentworks
silentworks2mo ago
But you mentioned React+Vite which would signify a client side app, why would this be using cookies? So you are seeing the cookie being set and then cleared?
Wilson Ibekason
Wilson IbekasonOP2mo ago
I dropped more detailed code to provide better context
silentworks
silentworks2mo ago
This is just a bunch of files that won't make much sense to me as It's not my application.
Wilson Ibekason
Wilson IbekasonOP2mo ago
Exactly, immediately it's set, it's been cleared out after some couple of secs 6-10sec
silentworks
silentworks2mo ago
I was asking about the .onAuthStateChange as it doesn't like when you do await inside of it as stated in the docs and this can cause odd behaviour. You mentioned you are using cookies but none of the Gists your povide shows it using cookies. Your client is created here https://gist.github.com/wilsonibekason/56be11ce7b74def755381a21139c175d#file-client and there is no cookie configuration in it. I would suggest that you create a GitHub repo with a minimal reproducible example. This way it will be easier for someone to look into it.
Wilson Ibekason
Wilson IbekasonOP2mo ago
Could u share me some docs reference
silentworks
silentworks2mo ago
If you look in the Javascript reference docs on the Supabase website you will see a note in the onAuthStateChange method.
Wilson Ibekason
Wilson IbekasonOP2mo ago
This is actually my forst time integrating with react, I previously do integrate with next, so is kinda confusing Ok
silentworks
silentworks2mo ago
I have a react example here you can take a look into https://github.com/silentworks/supabase-by-example/tree/main/react
Wilson Ibekason
Wilson IbekasonOP2mo ago
Can u provide me some practices on integrating supabase Auth on a react app, I can go ahead and debug it myself. Thanks

Did you find this page helpful?