-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
55 lines (41 loc) · 2.29 KB
/
passport.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
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Passport authentication */
/* */
/* see */
/* - passportjs.org/guide/configure */
/* - passportjs.org/guide/username-password */
/* - toon.io/understanding-passportjs-authentication-flow */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
'use strict';
const co = require('co'); // generator async control flow goodness
const passport = require('koa-passport'); // authentication
const LocalStrategy = require('passport-local').Strategy;
const User = require('./models/user.js');
// serialise user: record authenticated user's id in session
passport.serializeUser(function(user, done) {
done(null, user.UserId);
});
// deserialise user: restore user details to this.passport.user from id stored in session
passport.deserializeUser(function(id, done) {
// koa-passport can't deserialize through generator functions, so use co to wrap yieldable calls
co(function*() {
// lookup user
const user = yield User.get(id);
return user || null;
}).then(function(result) { done(null, result); }, done);
});
// use local strategy - passportjs.org/guide/username-password
passport.use(new LocalStrategy(function(username, password, done) {
// LocalStrategy doesn't know about generator functions, so use co to wrap yieldable calls
co(function*() {
// lookup user
const users = yield User.getBy('Email', username);
if (users.length == 0) return false; // user not found
const user = users[0];
// verify password matches
const match = (password ===user.Password);
if (!match) return false; // no password match
// validated ok, record return user details
return user;
}).then(function(result) { done(null, result); }, done);
}));