-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
109 lines (96 loc) · 2.89 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require("dotenv").config();
const passport = require('passport');
const JwtStrategy = require('passport-jwt').Strategy;
const { ExtractJwt } = require('passport-jwt');
const LocalStrategy = require('passport-local').Strategy;
const GooglePlusTokenStrategy = require('passport-google-oauth20').Strategy;
const User = require('./models/user');
const bcrypt = require('bcrypt');
passport.serializeUser(function(user, done){
done(null, user.id)
})
passport.deserializeUser(async function(id, done){
try{
var user = await User.findById(id)
done(null, user)
}catch(error){
done(error, null)
}
})
// JSON WEB TOKENS STRATEGY
passport.use(new JwtStrategy({
jwtFromRequest: ExtractJwt.fromHeader('authorization'),
secretOrKey: process.env.JWT_SECRET
}, async (payload, done) => {
try {
// Find the user specified in token
const user = await User.findById(payload.sub);
// If user doesn't exists, handle it
if (!user) {
return done(null, false);
}
// Otherwise, return the user
done(null, user);
} catch(error) {
done(error, false);
}
}));
// Google OAuth Strategy
passport.use('googleToken', new GooglePlusTokenStrategy({
callbackURL: 'https://cipher-mailbot.herokuapp.com/user/oauth/google/redirect',
clientID: process.env.clientID,
clientSecret: process.env.clientSecret
}, async (accessToken, refreshToken, profile, done) => {
// console.log(profile);
// return done(null, profile);
try {
// Should have full user profile over here
// console.log('profile', profile);
// console.log('accessToken', accessToken);
// console.log('refreshToken', refreshToken);
const existingUser = await User.findOne({ "google.id": profile.id });
if (existingUser) {
return done(null, existingUser);
}
const newUser = new User({
method: 'google',
name: profile.displayName,
createdAt: new Date().getTime(),
google: {
id: profile.id,
email: profile.emails[0].value
}
});
await newUser.save();
done(null, newUser);
} catch(error) {
done(error, false, error.message);
}
}));
// LOCAL STRATEGY
passport.use(new LocalStrategy({
usernameField: 'email'
}, async (email, password, done) => {
try {
// Find the user given the email
const user = await User.findOne({ "local.email": email });
// If not, handle it
if (!user) {
console.log("err1");
return done(null, false);
}
// Check if the password is correct
const isMatch = await bcrypt.compare(password, user.local.password);;
// If not, handle it
if (!isMatch) {
// console.log(user.local.password);
console.log("err2");
return done(null, false);
}
// Otherwise, return the user
done(null, user);
} catch(error) {
console.log("err3");
done(error, false);
}
}));