Created
May 2, 2023 14:11
-
-
Save robbypambudi/3eb845e75797c2043023a24e9649b7a5 to your computer and use it in GitHub Desktop.
This file contains 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'); | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
refrensi :