imaginar/utils/auth.js

21 lines
433 B
JavaScript
Raw Normal View History

2024-10-19 11:24:54 +00:00
const jwt = require('jsonwebtoken');
// JWTトークンが有効かどうかをチェックするモジュール
exports.isAuthenticated = (req, res, next) => {
const token = req.cookies.jwt;
if (!token) {
return res.redirect('/');
}
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
if (err) {
return res.redirect('/');
}
req.user = decoded;
req.token = token;
next();
});
};