Skip to content
Docs
Handling errors

Handling Errors

handleInvalidToken

The Auth middleware provides a handleInvalidToken function, which is called with an InvalidTokenReason as the first argument.

The InvalidTokenReason is primarily for informational purposes. The handleInvalidToken function is typically called when something expected happens, allowing the user to be safely redirected to the login page. One common expected event is when a user visits your app for the first time.

InvalidTokenReason

The table below describes the different types of InvalidTokenReason:

NameDescription
MISSING_CREDENTIALSThe request does not contain an authentication cookie
MISSING_REFRESH_TOKENCredentials have expired, and no refresh token is available
MALFORMED_CREDENTIALSThe cookies cannot be parsed or the structure has changed
INVALID_SIGNATUREThe cookie signature cannot be verified or the signature keys have changed
INVALID_CREDENTIALSThe cookies have a valid structure, but the idToken cannot be verified
INVALID_KIDThis error usually means the certificate used to sign the token has expired, which is expected. Google periodically refreshes certificates as part of key rotation (opens in a new tab)

handleError

Unlike handleInvalidToken, which handles expected issues, handleError is called when something unexpected occurs that a developer should investigate.

The handleError function receives an AuthError object as the first argument. This object includes code and message properties, which describe the type and meaning of the error.

The error codes are divided as follows:

CodeDescription
USER_NOT_FOUNDThe user cannot be found, possibly because they were removed after generating or refreshing the custom token
INVALID_CREDENTIALThe token could not be refreshed due to an invalid refresh token or service account credentials
TOKEN_EXPIREDHandled internally to refresh the token. Occurs when the custom idToken has expired
USER_DISABLEDThrown when authMiddleware is called with checkRevoked: true and the user has been disabled
TOKEN_REVOKEDThrown when authMiddleware is called with checkRevoked: true and the token has been revoked
INVALID_ARGUMENTThe token has an incorrect structure or the certificate used to sign it has expired
INTERNAL_ERRORAn internal error occurred. Check the error message for more details
NO_KID_IN_HEADERHandled internally to verify the token against all public certificates. Re-throws INVALID_SIGNATURE if none of the public keys match the token's signature
INVALID_SIGNATUREThe token signature cannot be verified
MISMATCHING_TENANT_IDProvided tenant ID does not match Firebase tenant ID from the token