Skip to content

Instantly share code, notes, and snippets.

@Mirch
Created November 30, 2018 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mirch/8489ac3e320fddb793669b71c08d7e37 to your computer and use it in GitHub Desktop.
Save Mirch/8489ac3e320fddb793669b71c08d7e37 to your computer and use it in GitHub Desktop.
// FORMAT OF TOKEN
// Authorization: Bearer <access_token>
// Verify Token
function verifyToken(req, res, next) {
// Get auth header value
const bearerHeader = req.headers['authorization'];
// Check if bearer is undefined
if (typeof bearerHeader !== 'undefined') {
// Split at the space
const bearer = bearerHeader.split(' ');
// Get token from array
const bearerToken = bearer[1];
// Set the token
req.token = bearerToken;
// Next middleware
next();
} else {
// Forbidden
res.sendStatus(403);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment