Created
June 29, 2020 22:58
-
-
Save leofmds/242e86f1db0692374b8ba5733491b53f to your computer and use it in GitHub Desktop.
express middleware to check valid firebase authentication
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
| let firebaseAuth = (req, res, next) => { | |
| let authHeader = req.get("Authorization"), bearer = "Bearer "; | |
| if (typeof authHeader === "undefined" || !authHeader.startsWith(bearer)) { | |
| return res.sendStatus(403); | |
| } | |
| let token = authHeader.substr(bearer.length); | |
| firebaseAdmin.auth().verifyIdToken(token).then((decodedToken) => { | |
| res.locals.uid = decodedToken.uid; | |
| next(); | |
| }).catch(err => { | |
| console.error('_firebaseAuth', err); | |
| return res.sendStatus(401); | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment