Skip to content

Instantly share code, notes, and snippets.

@clubrob
Created August 6, 2018 15:38
Show Gist options
  • Save clubrob/92efa08228263b94a6e25e91265e5c51 to your computer and use it in GitHub Desktop.
Save clubrob/92efa08228263b94a6e25e91265e5c51 to your computer and use it in GitHub Desktop.
Simple JWT auth middleware function
// Firebase token auth middleware function to protect routes
function authorizeMe(req, res, next) {
let idToken;
// Grab token from POST header
if (
req.headers.authorization &&
req.headers.authorization.startsWith('Bearer ')
) {
idToken = req.headers.authorization.split('Bearer ')[1];
// Firebase function to verify token
admin
.auth()
.verifyIdToken(idToken)
.then(decoded => decoded)
.then(() => next())
.catch(err => {
console.error(err.message);
res.status(403).send('Unauthorized');
});
} else {
res.status(403).send('Not Allowed');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment