nextauthjs / nextauthjs/next-auth

next-auth v4 session.update() not working right after signin

Open
#13,173 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug triage
Dominant language
TypeScript
Stars
28.4k
Forks
4k
PR merge metrics
No merged PRs in 30d

Description

Environment
next-auth@^4.24.11:
  version "4.24.11"
  resolved "https://registry.npmjs.org/next-auth/-/next-auth-4.24.11.tgz#16eeb76d37fbc8fe887561b454f8167f490c381f"

  System:
    OS: macOS 14.0
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 9.30 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v20.10.0/bin/yarn
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
  Browsers:
    Safari: 17.0

Reproduction URL

https://github.com/hehehai/next-auth-otp

Describe the issue

Because neither session and loading changed after login.

Image

session.update will bail out on the first conditional because session is null.

https://github.com/nextauthjs/next-auth/blob/46e01af41fc028f22322f225f4f03c0dce069035/packages/next-auth/src/react/index.tsx#L472

How to reproduce

Add

session.update(); // also add const session = useSession();

here
https://github.com/hehehai/next-auth-otp/blob/5eae1afbab3212f352b6fdd2f3530d2de9580188/src/app/login/page.tsx#L33-L35

The update function is not going through. And session not updated.

Expected behavior

The session should be updated.

It seems fixed in v5. So I guess it should also be fixed in v4?
https://github.com/nextauthjs/next-auth/blob/39dd3b92de194c1a835f2d87631f4deb9d9fdf65/packages/next-auth/src/react.tsx#L514

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with packages/next-auth/src/react/index.tsx around line 472 and reproduce the behavior using src/app/login/page.tsx at lines 33-35 from the linked example. Compare the v4 behavior with the referenced v5 implementation, then verify that session.update() runs after sign-in and that the session is updated.

Written by the indexing model from the issue text.

Assessment

Tech stack
nextjs, react, typescript
Domain
authentication, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.