-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
180 lines (157 loc) · 6.88 KB
/
index.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
177
178
179
180
const passport_ = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcryptjs');
module.exports = class Auth {
constructor(router) {
this.router = router
}
check(user, password){
return bcrypt.compareSync(password, user.password);
}
hash(password) {
return bcrypt.hashSync(password, 12);
}
commonExpressSetup(app, User) {
this.configureSessions((passport) => {
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
});
const registerUser = (req, username, password, done) => {
let findOrCreateUser = () => {
// find a user in Mongo with provided username
User.findOne({'username':username}, (err, user) => {
// In case of any error return
if (err){
console.log('Error in SignUp: '+err);
return done(err);
}
// already exists
if (user) {
console.log('User already exists');
return done(null, false, req.flash('message','User Already Exists'));
}
// if there is no user with that email
// create the user
var newUser = new User();
// set the user's local credentials
newUser.username = username;
newUser.password = this.hash(password);
newUser.email = req.param('email');
newUser.created_at = moment().format('YYYY-MM-DD HH:mm:ss')
// save the user
newUser.save(function(err) {
if (err){
console.log('Error in Saving user: '+err);
throw err;
}
console.log('User Registration succesful');
return done(null, newUser);
});
});
};
// Delay the execution of findOrCreateUser and execute
// the method in the next tick of the event loop
};
const loginUser = (req, username, password, done) => {
// check in mongo if a user with username exists or not
User.findOne({ 'username' : username }, (err, user) => {
// In case of any error, return using the done method
if (err) return done(err);
// Username does not exist, log error & redirect back
if (!user){
console.log('User Not Found with username ' + username);
return done(null, false, req.flash('message', 'User Not found.'));
}
// User exists but wrong password, log the error
if (!this.check(user, password)){
console.log('Invalid Password');
return done(null, false, req.flash('message', 'Invalid Password'));
}
// User and password both match, return user from
// done method which will be treated like success
return done(null, user);
});
}
// We need the user model, a login function, and a register function.
return {
loginUser,
registerUser
}
}
// This allows this developer implementing this package
// to configure their sessions however they please
configureSessions(callback){
callback(passport_);
}
// This binds the passed loginUser and register user functions to
// passport's respective declarations
setup(UserModel, loginUser, registerUser) {
passport_.use(new LocalStrategy(function(request, username, password, done) {
loginUser(request, username, password, done);
}));
passport_.use('signup', new LocalStrategy({ passReqToCallback: true, failureRedirect: '/register', successRedirect: '/home' }, function(req, username, password, done) {
let nextTicketRegisterUser = function () {
registerUser(req, username, password, done);
};
// Delay the execution of findOrCreateUser and execute
// the method in the next tick of the event loop
process.nextTick(nextTicketRegisterUser);
}));
}
// This binds the routes to the router object.
routes(config) {
if (!config.hasOwnProperty('getLogin') && typeof config.getLogin !== 'function') {
throw new Error ("You must declare a getLogin function in your auth router config")
}
if (!config.hasOwnProperty('getRegister') && typeof config.getRegister !== 'function') {
throw new Error ("You must declare a getRegister function in your auth router config")
}
if (!config.hasOwnProperty('registerRedirect') && typeof config.registerRedirect !== 'string') {
throw new Error ("You must declare a registerRedirect string in your auth router config")
}
this.router.get('/login', config.getLogin);
this.router.post('/login', passport_.authenticate('local', { successRedirect: '/home', failureRedirect: '/login', failureFlash: true }), (req, res) => {
res.redirect('/home')
});
this.router.get('/register', config.getRegister);
this.router.post('/register', passport_.authenticate('signup', {
successRedirect: config.registerRedirect,
failureRedirect: '/register',
failureFlash : true
}));
}
// The protect middleware binding.
protect(callback) {
// if the callback isn't defined default to one which just makes sure
// someone is logged in.
let middleware = callback ? callback : (request, response, next) => {
if (!request.isAuthenticated())
return response.redirect('/login');
return next();
}
// get local this because of fucked up scope.
let that = this;
return {
get(path, method) {
that.router.get(path, method, middleware);
},
post(path, method) {
that.router.post(path, method, middleware);
},
put(path, method) {
that.router.put(path, method, middleware);
},
delete(path, method) {
that.router.delete(path, method, middleware);
}
}
}
}