You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
594 B
26 lines
594 B
const { verifyToken } = require('../utils/jwt');
|
|
|
|
const authenticateToken = (req, res, next) => {
|
|
const authHeader = req.headers['authorization'];
|
|
const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
|
|
|
|
if (!token) {
|
|
return res.status(401).json({
|
|
code: 401,
|
|
msg: '访问被拒绝,缺少令牌'
|
|
});
|
|
}
|
|
|
|
try {
|
|
const decoded = verifyToken(token);
|
|
req.user = decoded;
|
|
next();
|
|
} catch (error) {
|
|
return res.status(403).json({
|
|
code: 403,
|
|
msg: '令牌无效或已过期'
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports = { authenticateToken }; |