-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport-config.js
63 lines (54 loc) · 2.06 KB
/
passport-config.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 LocalStrategy = require('passport-local').Strategy
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const bcrypt = require('bcrypt')
require('dotenv').config();
function initialize(passport, getUserByEmail, getUserById) {
// TODO: Make login work with DB
const authenticateUser = async (email, password, done) => {
console.log(email)
let user = null
user = await getUserByEmail(email)
console.log(user)
if (user == null){
console.log("No user")
return done(null, false, { message: 'No user with that email'})
};
try {
if(await bcrypt.compare(password, user.password)) {
console.log("User authenticated.")
const body = { id: user.id, email: user.email };
return done(null, user)
} else {
console.log("Wrong password")
return done(null, false, { message: 'Password incorrect'})
};
} catch (e) {
return done(e)
};
};
console.log("Passport.use")
passport.use(new LocalStrategy({ usernameField: 'email' }, authenticateUser));
passport.serializeUser((user, done) => {
console.log("Serializing:", user.user_id)
done(null, user.user_id)});
passport.deserializeUser((id, done) => {
console.log(id);
return done(null, getUserById(id));
});
// Use the GoogleStrategy within Passport.
// Strategies in passport require a `verify` function, which accept
// credentials (in this case, a token, tokenSecret, and Google profile), and
// invoke a callback with a user object.
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
consumerSecret: process.env.GOOGLE_SECRET,
callbackURL: "/grants"
},
function(token, tokenSecret, profile, done) {
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return done(err, user);
});
}
));
};
module.exports = initialize