auth.js
2.43 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
80
var LocalStrategy = require('passport-local').Strategy,
FacebookStrategy = require('passport-facebook').Strategy,
User = require('../models/User');
module.exports = function(passport) {
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local-signin', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
}, function(req, email, password, done) {
process.nextTick(function () {
User.findOne({email: email}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, req.flash('danger', '존재하지 않는 사용자입니다.'));
}
if (!user.validatePassword(password)) {
return done(null, false, req.flash('danger', '비밀번호가 일치하지 않습니다.'));
}
return done(null, user, req.flash('success', '로그인되었습니다.'));
});
});
}));
passport.use(new FacebookStrategy({
// 이 부분을 여러분 Facebook App의 정보로 수정해야 합니다.
clientID : '1671397389833378',
clientSecret : '2ec10aa15990b01d0d1a2013e6d6e467',
callbackURL : 'http://localhost:3000/auth/facebook/callback',
profileFields : ["emails", "displayName", "name", "photos"]
}, function(token, refreshToken, profile, done) {
var email = profile.emails[0].value;
process.nextTick(function () {
User.findOne({'facebook.id': profile.id}, function(err, user) {
if (err) {
return done(err);
}
if (user) {
return done(null, user);
} else {
User.findOne({email: email}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
user = new User({
name: profile.displayName,
email: profile.emails[0].value
});
}
user.facebook.id = profile.id;
user.facebook.token = profile.token;
user.facebook.photo = profile.photos[0].value;
user.save(function(err) {
if (err) {
return done(err);
}
return done(null, user);
});
});
}
});
});
}));
};