node-go/packages/server/middleware/verifyJWT.js
2020-02-05 16:54:09 -08:00

24 lines
No EOL
608 B
JavaScript

const dotenv = require('dotenv');
const jwt = require('jsonwebtoken');
dotenv.config();
const verifyToken = async (req, res, next) => {
const token = req.cookies.token || '';
const secret = process.env.NODE_ENV === 'test' ? process.env.TEST_SECRET : process.env.JWT_SECRET;
try {
if (!token) {
return res.status(401).json('You need to Login')
}
const decrypt = await jwt.verify(token, secret);
req.user = {
id: decrypt.id,
username: username,
};
next();
} catch (err) {
return res.status(500).json(err.toString());
}
};
module.exports = verifyToken;