f12눌러서 쿠키에 세션은 잘 저장되어있는데,
콘솔로찍어보면 언디파인이 뜹니다.
우선 로그인 코드는 이렇습니다
app.post(
'/login',
passport.authenticate('local', {
failureRedirect: '/fail',
}),
function (req, res) {
res.redirect('/');
}
);
passport.use(
new LocalStrategy(
{
usernameField: 'id',
passwordField: 'pw',
session: true,
passReqToCallback: false,
},
function (id, pw, done) {
db.collection('login').findOne({ id: id }, function (err, data) {
if (err) return done(err);
if (!data)
return done(null, false, { message: '존재하지않는 아이디요' });
if (bcrypt.compare(pw, data.pw)) {
return done(null, data);
} else {
return done(null, false, { message: '비번틀렸어요' });
}
});
}
)
);
// 세션 저장 코드
passport.serializeUser(function (user, done) {
done(null, user.id);
});
// 세션 해석 코드
// 방문자가 세션아이디 쿠키가 존재하면 deserializeUser 라는 함수 덕분에 항상 req.user라는 데이터가 존재합니다.
// 그러면 DB에서 {id : 세션아이디에 숨겨져있던 유저의 아이디} 인 게시물을 하나 찾아서
//그 찾은 DB데이터 결과를 done(null, 결과) 이렇게 해줍니다.
passport.deserializeUser(function (userId, done) {
db.collection('login').findOne({ id: userId }, function (err, data) {
done(null, data);
});
});
그리고 로그인 확인 미들웨어
function isLogin(req, res, next) {
console.log(req.user);
if (req.user) {
next();
} else {
res.send('로그인 안하셨습니다.');
}
}
등등입니다.
왜 그런걸까요?..