-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
39 lines (34 loc) · 1.07 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
const passport = require('koa-passport');
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcryptjs');
const knex = require('./database/connections');
const options = {};
async function comparePassword(userPassword, dbPassword) {
return await bcrypt.compareSync(userPassword, dbPassword);
}
passport.serializeUser((user, done) => {
done(null, user.id);
})
passport.deserializeUser((id, done) => {
return knex('users').where({ id }).first()
.then(user => {
done(null, user);
}).catch(err=> {
done(err, null);
})
});
passport.use('local', new LocalStrategy((username, password, done) => {
return knex('users').where({ username }).first()
.then(user => {
if (!user) {
return done(null, false);
}
if (!comparePassword(password, user.password)) {
return done(null, false);
} else {
return done(null, user);
}
}).catch(err => {
return done(err);
});
}));