-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport-config.js
74 lines (68 loc) · 1.74 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
64
65
66
67
68
69
70
71
72
73
74
const passport = require("passport");
const config = require("./config");
const User = require("./models/users");
const LocalStrategy = require("passport-local").Strategy;
const FacebookStrategy = require("passport-facebook").Strategy;
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
const verifyLocal = async (email, password, done) => {
try {
const user = await User.findOne({ email });
if (!user) {
return done(null, false, {
message: "username or password is Invalid",
});
}
if (!user.validPassword(password)) {
return done(null, false, {
message: "Incorrect username or password",
});
}
return done(null, user);
} catch (error) {
console.error(error);
}
};
passport.use(
new LocalStrategy(
{
usernameField: "email",
},
verifyLocal
)
);
const verifyCallback = async (accessToken, refreshToken, profile, cb) => {
const user = await User.findOne({ facebookId: profile.id });
if (!user) {
console.log("Adding new user to DB");
const user = await User.create({
name: profile.name.givenName,
email: profile.emails[0].value,
facebookId: profile.id,
});
if (!user) {
return cb(null, false, { message: "Can't save user info" });
}
await user.save();
return cb(null, profile);
} else {
console.log("This User is Already exists");
return cb(null, profile);
}
};
passport.use(
new FacebookStrategy(
{
clientID: config.app_id,
clientSecret: config.app_secret,
callbackURL: config.app_callbackUrl,
profileFields: ["id", "name", "emails"],
enableProof: true,
},
verifyCallback
)
);