Skip to content

Commit

Permalink
feat(gladly-team#174): handle additional errors from verifyIdToken
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmannZ committed Nov 22, 2021
1 parent e1d80e0 commit 647ff84
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/firebaseAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import getFirebaseAdminApp from 'src/initFirebaseAdminSDK'
import createAuthUser from 'src/createAuthUser'
import { getConfig } from 'src/config'

// https://firebase.google.com/docs/auth/admin/errors
const FIREBASE_ERROR_TOKEN_EXPIRED = 'auth/id-token-expired'
const FIREBASE_ERROR_ARGUMENT_ERROR = 'auth/argument-error'

// If the FIREBASE_AUTH_EMULATOR_HOST variable is set, send the token request to the emulator
const getTokenPrefix = () =>
process.env.FIREBASE_AUTH_EMULATOR_HOST
Expand Down Expand Up @@ -60,17 +56,29 @@ export const verifyIdToken = async (token, refreshToken = null) => {
try {
firebaseUser = await admin.auth().verifyIdToken(token)
} catch (e) {
// If the user's ID token has expired, refresh it if possible.
if (
refreshToken &&
(e.code === FIREBASE_ERROR_TOKEN_EXPIRED ||
e.code === FIREBASE_ERROR_ARGUMENT_ERROR)
) {
newToken = refreshExpiredIdToken(refreshToken)
firebaseUser = await admin.auth().verifyIdToken(newToken)
} else {
// Otherwise, throw.
throw e
// https://firebase.google.com/docs/auth/admin/errors
switch (e.code) {
case 'auth/id-token-expired':
case 'auth/argument-error':
// If the user's ID token has expired, refresh it if possible.
if (refreshToken) {
newToken = await refreshExpiredIdToken(refreshToken)
firebaseUser = await admin.auth().verifyIdToken(newToken)
} else {
// Otherwise, throw.
throw e
}
break
case 'auth/invalid-user-token':
case 'auth/user-token-expired':
case 'auth/user-disabled':
// Return an unauthenticated user
newToken = null
firebaseUser = null
break
default:
// Otherwise, throw.
throw e
}
}
const AuthUser = createAuthUser({
Expand Down

0 comments on commit 647ff84

Please sign in to comment.