Created
January 27, 2019 11:24
-
-
Save zprima/b51bca8b82e158582574247633a7d616 to your computer and use it in GitHub Desktop.
medium_p1_c3
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
//... | |
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