Last active
September 27, 2020 14:57
-
-
Save jacobedawson/de1113840f510497a236bbf28715fe6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This middleware accesses the firebase id token in the request header. | |
Then we use the Firebase Admin SDK to verify the token. | |
If successful we pass the route on, otherwise we return an error | |
*/ | |
const admin = require("firebase-admin"); | |
const serviceAccount = require("../config/firebase-account.json"); | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: "https://YOUR_URL.firebaseio.com" | |
}); | |
exports.requireAuth = async (req, res, next) => { | |
const idToken = req.header("FIREBASE-AUTH-TOKEN"); | |
let decodedIdToken; | |
try { | |
decodedIdToken = await admin.auth().verifyIdToken(idToken); | |
} catch (error) { | |
next(error); | |
return; | |
} | |
req.user = decodedIdToken; | |
next(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment