forked from stackroute/newThemeTattva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport-init.js
executable file
·126 lines (112 loc) · 3.99 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
/*Copyright 2016 Wipro Limited, NIIT Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This code is written by Prateek Reddy Yammanuru, Shiva Manognya Kandikuppa, Uday Kumar Mydam, Nirup TNL, Sandeep Reddy G, Deepak Kumar*/
var mongoose = require('mongoose');
var User = require('./models/dbConfig.js').userModel;
var organizationModel = require('./models/dbConfig.js').organizationModel;
var fluentD = require('./routes/fluentdConfig.js')
var LocalStrategy = require('passport-local').Strategy;
var crypto = require('crypto');
module.exports = function(passport){
// Passport needs to be able to serialize and deserialize users to support persistent login sessions
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('login', new LocalStrategy({
passReqToCallback : true
},
function(req, username, password, done) {
// check in mongo if a user with username exists or not
User.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){
return done(null, false);
}
// User exists but wrong password, log the error
if (!(isValidPassword(user,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
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) {
// find a user in mongo with provided username
User.findOne({ 'username' : username }, function(err, user) {
// In case of any error, return using the done method
if (err){
return done(err);
}
// already exists
if (user) {
return done(null, false);
} else {
// if there is no user, create the user
var newUser = new User();
var hash=crypto.randomBytes(16).toString('base64');
// set the user's local credentials
newUser.username = username;
newUser.hash=hash;
newUser.password = crypto.pbkdf2Sync(req.body.password,hash, 10000, 64).toString('base64');
newUser.email=req.body.email;
newUser.firstName=req.body.firstName;
newUser.lastName=req.body.lastName;
newUser.organization=req.body.organization;
console.log(req.body.organization+"---------------");
// save the user
newUser.save(function(err) {
if (err){
throw err;
}
organizationModel.findOne({ 'organizationName' : newUser.organization }, function(err, organizationName) {
// In case of any error, return using the done method
if (err){
return done(err);
}
if(!organizationName){
var newOrganization= new organizationModel();
newOrganization.organizationName=newUser.organization;
newOrganization.save(function(err) {
//console.log(err+"error in organiz");
fluentD(newUser.organization);
});
}
});
return done(null, newUser);
});
}
});
})
);
var isValidPassword = function(user,password) {
if(user.password==crypto.pbkdf2Sync(password, user.hash, 10000, 64).toString('base64')){
return true;
}
else {
return false;
}
};
};