-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
74 lines (70 loc) · 2.18 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
const { jwtSecret, googleClientId, googleSecret } = require("./config");
const JwtStrategy = require("passport-jwt").Strategy,
ExtractJwt = require("passport-jwt").ExtractJwt;
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const User = require("./models/User");
// JWT Strategy
const opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: jwtSecret
};
passport.use(
new JwtStrategy(opts, async (payload, done) => {
try {
// find the user specified in token
const user = await User.findById(payload.id);
if (!user) {
return done(null, false);
}
done(null, user);
} catch (err) {
done(err, false);
}
})
);
// JWT ends
// GOOGLE OAUTH Strategy
// passport.use(
// new GoogleStrategy(
// {
// clientID: googleClientId,
// clientSecret: googleSecret,
// callbackURL: "http://localhost:3000"
// },
// async (accessToken, refreshToken, profile, done) => {
// console.log("accesstoken", accessToken);
// console.log("refreshToken", refreshToken);
// console.log("profile", profile);
// done(null, profile);
// }
// )
// );
//Local Strategy
passport.use(
new LocalStrategy(
{
usernameField: "email"
},
async (email, password, done) => {
try {
console.log("trying to lgin in passport");
//find the user from email
const user = await User.findOne({ email });
//if not handle
if (!user) {
return done(null, false);
}
// check if password is correct
const isValidPassword = await user.passwordValid(password);
if (!isValidPassword) {
return done(null, false);
}
done(null, user);
} catch (err) {
done(err, false);
}
}
)
);