Skip to content

Instantly share code, notes, and snippets.

@leofmds
Created June 29, 2020 22:58
Show Gist options
  • Select an option

  • Save leofmds/242e86f1db0692374b8ba5733491b53f to your computer and use it in GitHub Desktop.

Select an option

Save leofmds/242e86f1db0692374b8ba5733491b53f to your computer and use it in GitHub Desktop.
express middleware to check valid firebase authentication
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