accounts.js
2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var express = require('express');
var router = express.Router();
var UserModel = require('../models/UserModel');
var passwordHash = require('../libs/passwordHash');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function (user, done) {
console.log('serializeUser');
done(null, user);
});
passport.deserializeUser(function (user, done) {
var result = user;
result.password = "";
console.log('deserializeUser');
done(null, result);
});
passport.use(new LocalStrategy({
usernameField: 'username',
passwordField : 'password',
passReqToCallback : true
},
function (req, username, password, done) {
UserModel.findOne({ username : username , password : passwordHash(password) }, function (err,user) {
if (!user){
return done(null, false, { message: '아이디 또는 비밀번호 오류 입니다.' });
}else{
return done(null, user );
}
});
}
));
router.get('/', function(req, res){
res.send('account app');
});
router.get('/join', function(req, res){
res.render('accounts/join');
});
router.post('/join', function(req, res){
var User = new UserModel({
username : req.body.username,
password : passwordHash(req.body.password),
displayname : req.body.displayname
});
User.save(function(err){
res.send('<script>alert("회원가입 성공");location.href="/accounts/login";</script>');
});
});
router.get('/login', function(req, res){
res.render('accounts/login', { flashMessage : req.flash().error });
});
router.post('/login' ,
passport.authenticate('local', {
failureRedirect: '/accounts/login',
failureFlash: true
}),
function(req, res){
res.send('<script>alert("로그인 성공");location.href="/";</script>');
}
);
router.get('/success', function(req, res){
res.send(req.user);
});
router.get('/logout', function(req, res){
req.logout();
res.redirect('/accounts/login');
});
module.exports = router;