-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
97 lines (90 loc) · 2.33 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
const passport = require("passport");
const JWTstrategy = require("passport-jwt").Strategy;
const { ExtractJwt } = require("passport-jwt");
const GoogleToken = require("passport-google-plus-token");
const LocalStrategy = require("passport-local").Strategy;
const User = require("./models/User");
//jwt strategy
passport.use(
new JWTstrategy(
{
jwtFromRequest: ExtractJwt.fromHeader("authorization"),
secretOrKey: process.env.JWT_SECRET,
},
async (payload, done) => {
try {
//Find users scecified in token
const user = await User.findById(payload.sub);
//if user dosnt exist , handle it
if (!user) {
return done(null, false);
}
done(null, user);
} catch (error) {
done(error, false);
}
},
),
);
//local strategy
passport.use(
new LocalStrategy(
{
usernameField: "email",
},
async (email, password, done) => {
//find the user with email
try {
const user = await User.findOne({ email }); //very importent to await !!!
//case not handle if
if (!user || user.method !== "local") {
return done(null, false);
}
//check if password is correct
const isMatch = await user.isValidPassword(password);
//case not handle if
if (!isMatch) {
return done(null, false);
}
done(null, user);
} catch (error) {
done(error, false);
}
},
),
);
//google oauth strategy
passport.use(
"googleToken",
new GoogleToken(
{
clientID: process.env.GOOGLE_C_ID,
clientSecret: process.env.GOOGLE_SECRET,
},
async (accessToken, refreshToken, profile, done) => {
try {
//try to find a user by email
const existingUser = await User.findOne({
email: profile.emails[0].value,
});
if (existingUser) {
console.log("got here");
return done(null, existingUser);
}
console.log("got to new user");
const newUser = new User({
method: "google",
email: profile.emails[0].value,
google: {
id: profile.id,
},
displayName: "",
});
await newUser.save();
done(null, newUser);
} catch (error) {
done(error, false);
}
},
),
);