next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

fix: allow updating session in SessionProvider when logged out

Open kpoelhekke opened this issue 4 months ago • 2 comments

☕️ Reasoning

Currently when after doing a signOut() request there is no way to update the session on the client side. If the session on the server is null setSession is never called. So the only way to make sure the client has an up to date value in the SessionProvider is to do a hard refresh.

Current:

  export const handleSignOutAction = async () => {
    "use server";
    return signOut({
      redirect: false,
    });
  };

  await handleSignOutAction();
  await session.update(); // currently after rerender useSession.session just returns the logged in session from the `SessionProvider`

After this PR:

  export const handleSignOutAction = async () => {
    "use server";
    return signOut({
      redirect: false,
    });
  };

  await handleSignOutAction();
  await session.update(); // after rerender useSession.session returns `null`

Potential problem that may arise is that fetchData on line 495 returns null when an the fetch errors. So In that case the SessionProvider will also be updated with null and the client will assume it's logged out.

🧢 Checklist

  • [x] Documentation
  • [x] Tests
  • [x] Ready to be merged

🎫 Affected issues

This issue is related, where it was not possible to update the session after loggin in: https://github.com/nextauthjs/next-auth/pull/11075

📌 Resources

kpoelhekke avatar Oct 03 '24 08:10 kpoelhekke