question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

signOut does not clear session and useUser does not get latest until refresh once

See original GitHub issue

Bug report

Describe the bug

Hello, I am currently upgrading from v1 to v2 and noticed some weird behaviors with auth-helpers and Nextjs after the exciting 0.5.1 update!

I have followed the migration guide from both supabase and auth-helpers.

There are two weird behaviors:

  • Once I login with email+password, useUser still returns null on my /home page until I refresh (waiting does nothing). I have done a session check on a middleware.ts before home loads, so I am sure that the user is valid/logged in.
// login.ts
const {
      data: { user },
      error,
    } = await supabaseClient.auth.signInWithPassword({
      email,
      password,
    });

router.push('/home');

// home.ts
const user = useUser(); // null
  • Once I sign out, /home -> / (server-side checks that no session) -> /login, the useUser on the /login page still detects User active and SessionContext has non-null Session object until I refresh (waiting does nothing).
if (user) { const { error } = await supabaseClient.auth.signOut(); router.push('/'); }

Expected behavior

useUser returns null THEN User when redirected to /home. signOut to clear session and useUser to return null instead of User.

System information

  • OS: Windows
  • Browser: Firefox
  • Version of Node.js: LTS 16

Version:

"@supabase/auth-helpers-nextjs": "0.5.1",
"@supabase/auth-helpers-react": "0.3.1",
"@supabase/supabase-js": "2.0.5",
"next": "12.3.1",
"react": "^18.2.0",
// Also tried with default Component prop with similar result
type ExtendedAppProps = AppProps<{ initialSession: Session }> & {
  Component: NextPage<{ initialSession: Session }>;
  emotionCache: EmotionCache;
};

const App = (props: ExtendedAppProps) => {
<SessionContextProvider
          supabaseClient={supabaseClient}
          initialSession={pageProps.initialSession}
        >
<Component {...pageProps} />
// ...

Additional context

I have seen other threads in Discord, but they were v1.

For github, this is half of my problem (no one answered) https://github.com/supabase/supabase/discussions/9558

There is another thread https://github.com/supabase/supabase/discussions/9639 which gary has answered but exhibits similar problem.

Potential alternatives:

  • getSession instead as that will always get latest
    • Con: slower and may re-run each render

Current workaround: stop using useUser or SessionContext in checks or critical parts. Move all checks server-side in middleware.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
j4w8ncommented, Nov 13, 2022

@NanoCode012 @AvidDabbler @vincentjflorio @thorwebdev

Regarding the signOut, this seems to be a gotrue-js issue. I was able to resolve with a local code change, but wasn’t confident enough to create a PR for it. Hopefully a maintainer can verify or not.

https://github.com/supabase/gotrue-js/issues/524

0reactions
zakrza59commented, Dec 4, 2022

Also have problem with this. I logout the user on one tab, on other tab I can see user logged in navbar where I use useUser hook. Any other ideas for this or should I feed the navbar from server side code?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Signout does not logout user when session is being requested ...
This was happening because on logout my application would redirect to a specific logout page where we check if the user is authenticated...
Read more >
ASP.NET Identity username won't refresh after signout/signin
The problem is step 3, after signin the user is still returned with the old username, even though in the db it's the...
Read more >
Supabase Auth with Next.js
With the update to supabase-js v2 the auth API routes are no longer required, therefore you can go ahead and delete your auth...
Read more >
Set ChromeOS device policies - Chrome Enterprise and ...
Sign in using your administrator account (does not end in @gmail.com). From the Admin console Home page, go to Devices and then Chrome....
Read more >
Django Tutorial Part 8: User authentication and permissions
In this tutorial, we'll show you how to allow users to log in to your site with their own accounts, and how to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found