payloadcms / payloadcms/payload

autoLogin user cannot refresh token when sessions are enabled

Open
#17,249 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

created-by: Contributor
Dominant language
TypeScript
Stars
44.8k
Forks
4.2k
Avg merge
2d 21h
Merged PRs (30d)
53

Description

Describe the Bug

With admin autoLogin enabled, the admin UI can appear logged in, but a forced token refresh fails with 403 when the auth collection uses the default useSessions: true.

Observed request:

POST /api/users/refresh-token?refresh 403

This happened on Payload 3.80.0 with config like:

admin: {
  user: Users.slug,
  autoLogin: {
    email: 'user@example.com',
    password: 'user@example.com',
  },
  autoRefresh: true,
}

Some of our code called refreshCookie(true). This resulted in a 403->autoLogin->403 loop.

Removing autoLogin / autoRefresh and doing a real login fixed it.

Suspected Cause

autoLogin in auth/strategies/jwt returns a user object, but does not create a session, sign a JWT, set a cookie, or attach _sid.

For auth collections, useSessions defaults to true. The refresh operation then requires req.user._sid:

  • JWTAuthentication.autoLogin returns only { user }
  • real login creates a session via addSessionToUser and signs sid into the JWT
  • refreshOperation throws Forbidden when sessions are enabled and _sid is missing

So the admin can be treated as authenticated via auto-login, but refresh-token cannot refresh because there is no session-backed token to refresh.

Expected Behavior

autoLogin should create a refreshable session/token compatible with refresh-token

Environment

Payload 3.80.0

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 in auth/strategies/jwt at JWTAuthentication.autoLogin, then trace addSessionToUser and refreshOperation to compare auto-login with real login when useSessions is enabled. Done means autoLogin produces a session-backed refreshable token and the forced refresh no longer returns 403 or enters the reported loop.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
authentication
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.