imaginar/utils/auth.js
2024-10-19 20:24:54 +09:00

21 lines
433 B
JavaScript

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();
});
};