-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpassport.js
75 lines (68 loc) · 2.06 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var passport = require('passport');
var bcrypt = require('bcrypt-nodejs');
const LocalStrategy = require('passport-local').Strategy
var db = require('./models');
const config = require('./config');
const jwt = require('jsonwebtoken');
const authenticate = (username, password, done) =>{
db.reps.find({
where:{
rep_username: username
}
}).then(rep => {
if (!rep || !bcrypt.compareSync(password, rep.rep_password)) {
console.log("failed to login, username not found.");
return done(null, false, {message: 'invalid username/or and password combination'});
}
const payload = {
sub: rep.rep_id
};
// create a token string
const token = jwt.sign(payload, config.jwtSecret);
const data = {
username: rep.rep_username,
token: token
};
return done(null, data);
})
.catch((err)=> done(null,err)) // pass the error back
}
const register = (req, email, password, done) => {
//sequelize?
db.reps.find({
where:{
rep_username: req.body.username,
}
})
.then(user => {
if (user) {
return done(null, false, { message: 'an account with that email has already been created' });
}
if (password !== req.body.password) {
return done(null, false, { message: `passwords don't match` });
}
db.reps.create({
rep_username: req.body.username,
rep_email: req.body.email,
rep_password: bcrypt.hashSync(password)
}).then(function(){
done(null, user);
})
})
}
passport.use(new LocalStrategy(authenticate));
passport.use('local-register', new LocalStrategy({passReqToCallback: true}, register));
// passport.serializeUser((reps, done) => {
// done(null, reps.rep_id)
// });
// passport.deserializeUser((id, done) => {
// db.reps.find({
// where:{
// rep_id: id,
// }
// })
// .then(rep => {
// done(null, rep)
// })
// .catch(done)
// })