#Custom login + rememberMe not working (token expires early?)

2 messages · Page 1 of 1 (latest)

flint tundra
#

Hi, I'm using custom login with req.payload.login() and manually setting Max-Age for a rememberMe feature (30 days vs 1 hour).

However, users get logged out after about 1 hour even when Max-Age is 30 days.

From the types, my login() options do not support expiration, so I think JWT expiration is controlled only by auth.tokenExpiration in the collection config.

Is there a proper way to implement dynamic token expiration per login (rememberMe) in my Payload version?

Or is the only solution to set a global tokenExpiration?

Here's my code

export async function login(req: PayloadRequest) {
  if (!req.json) {
    throw new Error('Request body is not a JSON object')
  }

  const body = await req.json()
  const { email, password, rememberMe } = body

  const result = await req.payload.login({
    collection: 'users',
    data: { email, password },
  })

  const expiration = rememberMe ? 60 * 60 * 24 * 30 : 60 * 60

  console.log('expiration', expiration)

  const response = Response.json(result)

  response.headers.append(
    'Set-Cookie',
    `payload-token=${result.token}; HttpOnly; Path=/; Max-Age=${expiration}; SameSite=Lax`,
  )

  return response
}