forked from eloquence/lib.reviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
59 lines (51 loc) · 1.21 KB
/
auth.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
'use strict';
// External deps
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
// Internal deps
const User = require('./models/user');
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User
.getWithTeams(id)
.then(user => {
done(null, user);
})
.catch(error => {
done(error);
});
});
passport.use(new LocalStrategy(
function(username, password, done) {
User
.filter({
canonicalName: User.canonicalize(username)
})
.limit(1)
.then(users => {
if (!users.length)
return done(null, false, {
message: 'bad username'
});
let user = users[0];
user
.checkPassword(password)
.then(result => {
if (!result)
return done(null, false, {
message: 'bad password'
});
else
return done(null, user);
})
.catch(error => { // Problem with password check
done(error);
});
})
.catch(error => { // Problem with query
done(error);
});
}
));