Express ์๋ฒ๋ฅผ ์ํ ์๋์ค๋ฝ์ง ์๊ณ ๊ฐ๋จํ ์ฌ์ฉ์ ์ธ์ฆ ๋ฏธ๋ค์จ์ด
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
// ๋ฌธ์ ๊ฐ ์๋ค๋ฉด ์๋ฌ๋ฅผ ๋ฐํํ๋ค.
if (err) { return done(err); }
// ๊ฐ์
๋์ง ์์ ์ ์ ๋ผ๋ฉด false๋ฅผ ๋ฐํํ๋ค.
if (!user) { return done(null, false); }
// ๋น๋ฐ๋ฒํธ๊ฐ ํ๋ ธ๋ค๋ฉด false๋ฅผ ๋ฐํํ๋ค.
if (!user.verifyPassword(password)) { return done(null, false); }
// ์์ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ํต๊ณผํ๋ค๋ฉด, user๋ฅผ ๋ฐํํ๋ค.
return done(null, user);
});
}
));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
var app = express();
app.use(require('serve-static')(__dirname + '/../../public'));
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
// ์๋์ passport.initialize๋ฅผ ๋ฐ๋์ ํด์ค์ผํ๋ค.
app.use(passport.initialize());
app.use(passport.session());
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});