-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport-init.js
128 lines (109 loc) · 4.32 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
var LocalStrategy = require('passport-local').Strategy;
//var FacebookStrategy = require('passport-facebook').Strategy;
//var env_config = require('./helpers/common').config();
var bCrypt = require('bcrypt-nodejs');
var dbCon = require('./db');
var myCollection;
dbCon.getConnection(function(err, db){
myCollection = db.collection('user');
});
module.exports = function(passport){
// Passport needs to be able to serialize and deserialize users to support persistent login sessions
passport.serializeUser(function(user, done) {
return done(null, user);
});
passport.deserializeUser(function(username, done) {
return done(null, username);
});
passport.use('login', new LocalStrategy({
passReqToCallback : true
},
function(req, username, password, done) {
myCollection.findOne({ 'username' : username },
function(err, user) {
// In case of any error, return using the done method
if (err)
return done(err);
// Username does not exist, log the error and redirect back
if (!user){
console.log('User Not Found with username '+username);
return done(null, false);
}
// User exists but wrong password, log the error
if (!isValidPassword(user, password)){
console.log('Invalid Password');
return done(null, false); // redirect back to login page
}
// User and password both match, return user from done method
// which will be treated like success
req.session.username = username;
return done(null, user);
}
);
}
));
passport.use('signup', new LocalStrategy({
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, username, password, done) {
myCollection.findOne({ 'username' : username }, function(err, user) {
// In case of any error, return using the done method
if (err){
console.log('Error in SignUp: '+err);
return done(err);
}
// already exists
if (user) {
console.log('User already exists with username: '+username);
return done(null, false);
} else {
var hashPwd = createHash(password);
myCollection.insert({username : username,
password : hashPwd},function(err, res){
if (err) throw err;
return done(null,res);
});
}
});
}
));
/*passport.use('facebook', new FacebookStrategy({
clientID: env_config.facebook_app_id,
clientSecret: env_config.facebook_app_secret,
callbackURL: "http://localhost:3000/api/chat"
},
function(accessToken, refreshToken, profile, done) {
// find the user in the database based on their facebook id
// asynchronous
process.nextTick(function() {
// find the user in the database based on their facebook id
myCollection.findOne({ 'id': profile.id }, function(err, user) {
// if there is an error, stop everything and return that
// ie an error connecting to the database
if (err) done(err);
// if the user is found, then log them in
if (user) {
return done(null, user); // user found, return that user
} else {
console.log('insert profile in db');
// save our user to the database
myCollection.insert({id: profile.id,
name: profile.name
},
function(err, res){
if (err) done(err);
return done(null,res);
});
}
});
});
}
));*/
var isValidPassword = function(user, password){
return bCrypt.compareSync(password, user.password);
};
// Generates hash using bCrypt
var createHash = function(password){
return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
};
};