-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport-config.js
170 lines (163 loc) · 4.57 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const LocalStrategy = require("passport-local").Strategy;
const GitHubStrategy = require("passport-github2").Strategy;
const GoogleStrategy = require("passport-google-oauth2").Strategy;
const passport = require("passport");
const User = require("./models/users");
const bcrypt = require("bcrypt");
const initializePassport = async (passport, getUserByEmail, getUserById) => {
const authenticateUser = async (userEmail, password, done) => {
try {
const user = await getUserByEmail(userEmail);
if (user == null) {
return done(null, false);
}
//If the user exists and doesn't have a password, that means it was
//Created using oauth. Return an error here
if (!user.password) {
return done(null, false);
}
if (await bcrypt.compare(password, user.password)) {
return done(null, user);
} else {
return done(null, false);
}
} catch (err) {
console.log(err);
return done(err, false);
}
};
passport.use(
new LocalStrategy(
{
usernameField: "userEmail",
passwordField: "password",
},
authenticateUser
)
);
//const findOrCreate()
const handleOauthResponse = async (
externalWebsite,
id,
email,
name,
done
) => {
try {
let whereCondition = {};
switch (externalWebsite) {
case "github":
whereCondition = { githubId: id };
break;
case "google":
whereCondition = { googleId: id };
break;
}
//If a user with this github id exists, return this user
let userResponse = await User.findOne({
where: whereCondition,
});
if (userResponse) {
return done(null, userResponse);
}
/*
If a user with this github id does not exist, I want to create a new
user If a user with the same email already exists, return an error
*/
userResponse = await User.findOne({
where: { userEmail: email },
});
if (userResponse) {
return done(null, false);
}
const response = await User.findOrCreate({
where: whereCondition,
defaults: {
name: name,
userEmail: email,
rewardsPoints: 0,
},
});
return done(null, response[0]);
} catch (err) {
console.log(err);
return done(err, false);
}
};
passport.use(
new GitHubStrategy(
{
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: process.env.GITHUB_CALLBACK_URL,
},
async (accessToken, refreshToken, profile, done) => {
if (!profile.id || !profile.emails || !profile.displayName) {
return done(
new Error(
`Unable to retrieve the data required from GitHub to create an account. ` +
`Go to your profile settings, and make sure that your account id, email, ` +
`and name are all publicly accessible.`
),
false
);
}
await handleOauthResponse(
"github",
profile.id,
profile.emails[0].value,
profile.displayName,
done
);
}
)
);
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: process.env.GOOGLE_CALLBACK_URL,
passReqToCallback: true,
},
async (request, accessToken, refreshToken, profile, done) => {
if (!profile.id || !profile.emails || !profile.displayName) {
return done(
new Error(
`Unable to retrieve the data required from GitHub to create an account. ` +
`Go to your profile settings, and make sure that your account id, email, ` +
`and name are all publicly accessible.`
),
false
);
}
await handleOauthResponse(
"google",
profile.id,
profile.email,
profile.displayName,
done
);
}
)
);
passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser(async (id, done) =>
done(null, await getUserById(id))
);
};
initializePassport(
passport,
async (newUserEmail) =>
await User.findOne({
where: { userEmail: newUserEmail },
}),
async (newId) =>
await User.findOne({
where: { id: newId },
})
);
module.exports = passport;