Skip to content
Docs
Handling errors

Handling errors

handleInvalidToken

Auth middleware's handleInvalidToken function is called with InvalidTokenReason as the first argument.

The role of InvalidTokenReason is mostly informative. handleInvalidToken is usually called when something expected happens and we can safely redirect user to the login page. One of those expected events might be a user visiting your app for the first time.

InvalidTokenReason

Following table describes different types of InvalidTokenReason

NameDescription
MISSING_CREDENTIALSRequest does not contain authentication cookie
MISSING_REFRESH_TOKENCredentials have expired, but refresh token is not available
MALFORMED_CREDENTIALSCookies cannot be parsed or the structure has changed
INVALID_SIGNATURECookie signature cannot be verified or cookie signature keys have changed
INVALID_CREDENTIALSCookies have valid structure, but idToken cannot be verified

handleError

Contrary to handleInvalidToken, handleError is called when something unexpected happens. Something a developer should take a closer look at.

handleError is called with AuthError as the first argument. AuthError has code and message properties that describe the type and meaning of the error

It can be divided into following types

CodeDescription
USER_NOT_FOUNDUser cannot be found. Most likely was removed between generating and refreshing custom token
INVALID_CREDENTIALToken could not be refreshed due to invalid refresh token or service account credentials
TOKEN_EXPIREDHandled internally to refresh the token. Happens when 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_ARGUMENTToken has incorrect structure or certificate this token was signed with has expired
INTERNAL_ERRORInternal error. Check error message for details
NO_KID_IN_HEADERHandled internally to verify token against all public certificates. Re-throws INVALID_SIGNATURE if none of the public keys match token signature.
NO_MATCHING_KIDHandled internally to re-throw as INVALID_ARGUMENT. Usually indicates that certificate this token has been signed with has expired. This is expected. Google refreshes certificates periodically, which is a form of key rotation (opens in a new tab)
INVALID_SIGNATUREToken signature cannot be verified