-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
65 lines (51 loc) · 1.81 KB
/
auth.js
File metadata and controls
65 lines (51 loc) · 1.81 KB
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
var passport = require('passport');
var LocalStrategy = require('passport-local');
var sanitize = require('mongo-sanitize');
var bcrypt = require('bcrypt');
var mongoose = require('mongoose');
// connecting to db
var url = "mongodb://localhost:27017/tec_test";
mongoose.connect(url, { useMongoClient: true });
// require mongoose model (define in /model/models.js)
var models = require('./model/models')(mongoose);
// Passport session setup.
passport.serializeUser(function(user, done) {
console.log("serializing " + user);
done(null, user);
});
passport.deserializeUser(function(obj, done) {
console.log("deserializing " + obj);
done(null, obj);
});
// Use the LocalStrategy within Passport to login users.
passport.use('local-signin', new LocalStrategy(
//{ passReqToCallback : true }, //allows us to pass back the request to the callback
function(username, password, done) {
// prevent noSQL injection
username = sanitize(username);
password = sanitize(password);
console.log(username);
// look for the email
models.Coach.findOne({email: username}, function(err, result) {
console.log("USERNAME input :", username);
if (err) throw err;
// if no match retrun false (auth fail)
if (null == result) {
console.log("USERNAME NOT FOUND:", username);
done(null, false);
} else {
console.log("FOUND USER: " + result.email);
// compare pwd to pwd store in db
if (bcrypt.compareSync(password, result.password)){
console.log("LOGGED IN AS: " + result.email);
// sending back user mail as data for cookie (could be also id)
done(null, result.email);
} else {
console.log("AUTHENTICATION FAILED");
done(null, false);
}
}
});
}
));
module.exports = passport;