-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
63 lines (54 loc) · 1.28 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
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcryptjs');
const user = {
"username": 'Example',
"password": '1'
}
let cryptUsers = [];
const users = [
{
"username": "tiki",
"password": "pass1"
},
{
"username": "introfog",
"password": "passIntro"
},
{
"username": "Dimas",
"password": "pass2"
}
];
for (let i = 0; i < users.length; i++) {
bcrypt.genSalt(10, function (err, salt) {
bcrypt.hash(users[i]['password'], salt, function (err, hash) {
let crypUser = {
username: users[i]["username"],
password: hash,
}
cryptUsers.push(crypUser);
})
})
}
passport.use(
new LocalStrategy(function (username, password, done) {
let user = cryptUsers.find(j => j.username === username);
if (user === undefined) {
done(null, false);
}
if (bcrypt.compareSync(password, user.password)) {
done(null, user)
}
else {
done(null, false);
}
})
);
passport.serializeUser(function (user, done) {
done(null, user);
})
passport.deserializeUser(function (user, done) {
done(null, user);
})
module.exports = passport;