forked from Dryft-bits/ChronoFactorem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
59 lines (53 loc) · 1.52 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
const configuration = require("./server/config/constants.js");
const dotenv = require("dotenv");
dotenv.config();
const mongoose = require("mongoose");
const Student = mongoose.model("student");
const Login = mongoose.model("login");
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20");
const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID;
const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET;
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
Student.findById(id).then(user => {
done(null, user);
});
});
passport.use(
new GoogleStrategy(
{
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: configuration.urls.googleAuthCallback
},
(accessToken, refreshToken, profile, done) => {
Student.findOne({ email: profile.emails[0].value }).then(existingUser => {
if (existingUser) {
new Login({
userId: existingUser._id
}).save();
done(null, existingUser);
} else {
new Student({
googleId: profile.id,
name: profile.displayName,
email: profile.emails[0].value,
submittedForm: false,
branch: [],
year: 0
})
.save()
.then(user => {
new Login({
userId: user._id
}).save();
done(null, user);
});
}
});
}
)
);