21 lines
433 B
JavaScript
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();
|
||
|
});
|
||
|
};
|