imaginar/controllers/authController.js

32 lines
816 B
JavaScript
Raw Normal View History

2024-10-19 11:24:54 +00:00
const jwt = require('jsonwebtoken');
exports.handleCallback = async (req, res) => {
try {
if (!req.user.isAccountSetupComplete) {
res.redirect('/register');
} else {
const sessionExpiration = parseInt(process.env.SESSION_EXPIRATION, 10);
const token = jwt.sign(
{ userId: req.user.id },
process.env.JWT_SECRET,
{ expiresIn: sessionExpiration }
);
// JWTトークンをクッキーに保存
res.cookie('jwt', token, { httpOnly: true, maxAge: sessionExpiration * 1000 });
// Dashboardへリダイレクト
res.redirect('/dashboard');
}
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
};
exports.logout = (req, res) => {
req.logout(() => {
res.redirect('/');
});
};