-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassport-init.js
126 lines (107 loc) · 3.94 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
'use strict';
var LocalStrategy = require('passport-local').Strategy,
bCrypt = require('bcrypt-nodejs'),
// Mongo stuff
MongoClient = require('mongodb').MongoClient,
url = require('./dbConfig').url,
userCollection = 'user',
ObjectID = require('mongodb').ObjectID,
// End Mongo stuff
createHash, validPassword, dbUser;
createHash = function (password) {
return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
};
validPassword = function (user, password) {
return bCrypt.compareSync(password, user.password);
};
module.exports = function (passport) {
passport.serializeUser(function (user, done) {
console.log(JSON.stringify(user));
done(null, user._id);
});
passport.deserializeUser(function (id, done) {
MongoClient.connect(url, function (err, db) {
var collection;
if (err) {
console.log(err);
return done(err);
}
collection = db.collection(userCollection);
collection.findOne({_id: new ObjectID(id)}, function (err, user) {
db.close();
console.log(err);
return done(err, user);
});
});
});
passport.use('signup', new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true,
session: true
}, function (req, username, password, done) {
var signUpUser = function () {
MongoClient.connect(url, function (err, db) {
var collection;
if (err) {
return done(err);
}
collection = db.collection(userCollection);
collection.findOne({username: username}, function (err, user) {
if (err) {
db.close();
return done(err);
}
if (user) {
db.close();
console.log(username + ' already exists!');
return done(null, false, username + 'already exists!');
} else {
dbUser = {};
dbUser.username = username;
dbUser.password = createHash(password);
dbUser.email = req.body.email;
dbUser.firstName = req.body.firstName;
dbUser.lastName = req.body.lastName;
console.log(JSON.stringify(req.body));
collection.insert(dbUser, function (err, result) {
db.close();
return done(err, dbUser);
});
}
});
});
};
process.nextTick(signUpUser);
}));
passport.use('login', new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true,
session: true
}, function (req, username, password, done) {
MongoClient.connect(url, function (err, db) {
var collection;
if (err) {
return done(err);
}
collection = db.collection(userCollection);
collection.findOne({username: username}, function (err, user) {
db.close();
if (err) {
return done(err);
}
if (!user) {
console.log('User Not Found with username ' + username);
return done(null, false, 'User not found.');
}
if (!validPassword(user, password)) {
console.log('Invalid Password');
return done(null, false, 'Invalid Password');
}
console.log('User for passport: ' + JSON.stringify(user));
return done(null, user);
});
});
}));
};