Skip to content

Instantly share code, notes, and snippets.

@robbypambudi
Created May 2, 2023 14:11
Show Gist options
  • Save robbypambudi/3eb845e75797c2043023a24e9649b7a5 to your computer and use it in GitHub Desktop.
Save robbypambudi/3eb845e75797c2043023a24e9649b7a5 to your computer and use it in GitHub Desktop.
const jwt = require ('jsonwebtoken');
function generateAccessToken(user) {
return jwt.sign(user, process.env.TOKEN_SECRET);
}
function authenticateToken(req, res, next) {
const authHeader = req.headers['authorization']
const token = authHeader && authHeader.split(' ')[1]
if (token == null) return res.sendStatus(401)
jwt.verify(token, process.env.TOKEN_SECRET, (err, user) => {
console.log(err)
if (err) return res.sendStatus(403)
req.user = user
next()
})
}
module.exports = {
generateAccessToken,
authenticateToken
}
@robbypambudi
Copy link
Author

refrensi :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment