Skip to content

ID token refresh fails when using Persistence.Cookie auth persistence method #9018

Open
@PhastPhood

Description

@PhastPhood

Operating System

MacOS Sequoia Version 15.1.1

Environment (if applicable)

All browsers

Firebase SDK Version

11.6.1

Firebase SDK Product(s)

Auth

Project Tooling

React app with Next.js

Detailed Problem Description

Saw that there was a new Persistence.Cookie strategy for Auth in beta and decided to give it a try, since it should fix some Auth persistence issues we have in Safari. I'm mostly following the implementation from this PR.

It works great, except if a user logs in and doesn't close their browser for an hour, and the ID token expires, Firebase fails to refresh it. This logs the user out. Other persistence strategies don't seem to have this issue. I'm seeing that the error code is user-token-expired.

This seems to be because:

  1. When PersistenceUserManager.getCurrentUser is called, the user is created using the stored cookie value. This user has refreshToken = null since the cookie value is just a string with the ID token and does not have the refresh token.
  2. When the ID token is about to expire or expired and getIdToken refreshed, this line fails since the refresh token is null.

Steps and code to reproduce issue

  1. Set auth persistence to browserCookiePersistence:
auth.setPersistence(browserCookiePersistence);
  1. Set up Next.JS middleware to handle browser cookie persistence (example)
  2. Without closing the browser tab/window, wait an hour so that the ID token expires (or lower the refresh interval so that the force refresh happens more quickly)

Expected result:
User remains logged in, and ID token is refreshed. Firebase makes a request to /__cookies__ endpoint to refresh the ID token.

Actual behavior
The ID token expires and is not refreshed. The error code received is user-token-expired. A DELETE request is sent to /__cookies__ endpoint to log the user out.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions