forked from jerryh91/turnabout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport-init.js
executable file
·176 lines (151 loc) · 5.13 KB
/
passport-init.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var bCrypt = require('bcrypt-nodejs');
var mongoose = require('mongoose');
var User = mongoose.model('User');
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'whysosingle'
}
}, {
// default values for sendMail method
from: '[email protected]',
headers: {
'My-Awesome-Header': '123'
}
});
module.exports = function (passport)
{
//Authentication APIs
//Passport needs to be able to serialize and deserialize users
//to support persistent login sessions
//Passport requires that we provide a unique ID which
//specifies each user in order to serialize them into the session.
//TODO: Investigate deeper into purpose of de/serialize
passport.serializeUser(function(user, done)
{
//console.log('serializing user:',user._id);
return done(null, user._id);
});
passport.deserializeUser(function(id, done)
{
//console.log('deserializeUser');
//Find user with _id
User.findById (id, function (err, user)
{
if (err)
{
console.log('error');
return done(err, false);
}
if (!user)
{
console.log('user not found');
return done('user not found', false);
}
//return user obj to passport
//console.log('user found!');
return done(null, user);
})
});
passport.use('login', new LocalStrategy(
function(username, password, done)
{
User.findOne({'email' : username}, function(err, user){
if (err)
{
console.log("db error");
return done("db err: "+ err, false);
}
if (!user)
{
console.log("User doesn't exist in db");
//User doesn't exist in db
return done('user:' + username + 'NOT in db', false);
}
else if (!isValidPwd(user, password))
{
console.log('incorrect password');
return done('user: '+ username + 'incorrect password', false);
}
//Successful login
console.log('Successfully signed in ' + user.username);
return done(null, user);
});
}
));
passport.use('signup', new LocalStrategy({
passReqToCallback : true // allows us to pass back the entire request to the callback
},
//passport can parse
//username, password from a form body
function (req, username, password, done)
{
console.log("in signup");
//console.log(req);
// console.log('signup function callback');
User.findOne({'username': username}, function(err, user)
{
if (err)
{
console.log(err);
return done(err, false);
}
if (user)
{
console.log('user exists');
//User already in db
return done('user:' + username + 'already in db', false);
} else
{
console.log('creating new user');
//Create new user
var newUser = new User ();
newUser.username = username;
newUser.password = createHash(password);
newUser.email = req.body.email;
newUser.location = req.body.location;
newUser.age = req.body.age;
newUser.gender = req.body.gender;
newUser.survey = req.body.survey;
newUser.save(function(err, newUser)
{
if (err)
{
return (err, false);
}
console.log('Successfully added user: ' + username + 'db');
transporter.sendMail({
to: '[email protected]',
subject: 'hello',
text: 'hello world!'
});
return done(null, newUser);
});
}
});
// if(users[username])
// {
// return done('User :'+ username + 'already exists', false);
// }
// //Add new username:password
// users [username] =
// {
// username : username,
// password : createHash(password)
// };
// return done(null, users[username]);
})
);
var isValidPwd = function(user, password)
{
return bCrypt.compareSync(password, user.password);
};
var createHash = function(password)
{
return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
};
}