Skip to content

Instantly share code, notes, and snippets.

@zprima
Created January 27, 2019 11:24
Show Gist options
  • Save zprima/b51bca8b82e158582574247633a7d616 to your computer and use it in GitHub Desktop.
Save zprima/b51bca8b82e158582574247633a7d616 to your computer and use it in GitHub Desktop.
medium_p1_c3
//...
const jwt = require('jsonwebtoken');
const jwtSecret = "mysuperdupersecret"; // Use env for secrets
//...
// Auth middleware
app.use((req, res, next) => {
// login does not require jwt verification
if (req.path == '/api/login') {
// next middleware
return next()
}
// get token from request header Authorization
const token = req.headers.authorization
// Token verification
try {
var decoded = jwt.verify(token, jwtSecret);
console.log("decoded", decoded)
} catch (err) {
// Catch the JWT Expired or Invalid errors
return res.status(401).json({ "msg": err.message })
}
// next middleware
next()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment