Skip to content

Commit 4f11521

Browse files
committed
oauth works
1 parent 411eb1f commit 4f11521

File tree

8 files changed

+50
-16
lines changed

8 files changed

+50
-16
lines changed

app/src/components/login/SignIn.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = (props) => {
295295
<path d="M8.256 14a4.474 4.474 0 0 1-.229-1.004H3c.001-.246.154-.986.832-1.664C4.484 10.68 5.711 10 8 10c.26 0 .507.009.74.025.226-.341.496-.65.804-.918C9.077 9.038 8.564 9 8 9c-5 0-6 3-6 4s1 1 1 1h5.256Z" />
296296
</svg>
297297
</Button>
298-
{/* <Button
298+
<Button
299299
fullWidth
300300
id="SignInWithGithub"
301301
variant="contained"
@@ -318,7 +318,7 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = (props) => {
318318
>
319319
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z" />
320320
</svg>
321-
</Button>*/}
321+
</Button>
322322
<Button
323323
fullWidth
324324
variant="contained"

app/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ return (
6363
<Route
6464
{...rest}
6565
render={(props) => {
66-
if (isLoggedIn) {
66+
if (isLoggedIn === true) {
6767
console.log("should be app")
6868
// User is logged in, render the protected component
6969
return <Component {...props} />;

server/controllers/cookieController.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ const cookieController: CookieController = {
1212
//maxAge: 60 * 60 * 1000 * 24 //uncomment to set expiration of cookies, but make sure there is something in place to expire local storage info too
1313

1414
});
15+
16+
res.cookie('username', res.locals.username, {
17+
httpOnly: true,
18+
sameSite: 'none',
19+
secure: true,
20+
});
1521
return next();
1622
},
1723

server/controllers/sessionController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const sessionController: SessionController = {
2626
if (!session) {
2727
console.log('no session')
2828
res.locals.loggedIn = false;
29-
return res.redirect('/');
29+
return next();
30+
// return res.redirect('/');
3031
}
3132
res.locals.loggedIn = true;
3233
return next();

server/models/Oauth-model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const Schema = mongoose.Schema;
33

44
const userSchema = new Schema({
55
username: { type: String },
6-
githubId: { type: String, unique: false },
7-
googleId: { type: String, unique: false }
6+
githubId: { type: String, unique: true },
7+
googleId: { type: String, unique: true }
88
});
99

1010
const User = mongoose.model('OauthUsers', userSchema);

server/routers/auth.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,21 @@ router.get(
2626
router.get(
2727
'/github/callback',
2828
passport.authenticate('github'),
29+
sessionController.startSession,
2930
(req: UserReq, res) => {
30-
console.log('this authenticate function is being run');
31+
console.log('github authenticate function is being run');
3132
console.log(req.user.id);
32-
res.cookie('ssid', req.user.id);
33+
res.cookie('ssid', req.user.id, {
34+
httpOnly: true,
35+
sameSite: 'none',
36+
secure: true,
37+
});
38+
39+
res.cookie('username', req.user.username, {
40+
httpOnly: true,
41+
sameSite: 'none',
42+
secure: true,
43+
});
3344
return res.redirect(API_BASE_URL);
3445
}
3546
);
@@ -39,19 +50,24 @@ router.get(
3950
passport.authenticate('google', {
4051
scope: ['profile']
4152
})
53+
4254
);
4355

4456
router.get(
4557
'/google/callback',
4658
passport.authenticate('google'),
4759
sessionController.startSession,
4860
(req: UserReq, res) => {
61+
4962
console.log('google authenicate function being run');
5063
res.cookie('ssid', req.user.id, {
5164
httpOnly: true,
5265
sameSite: 'none',
5366
secure: true,
5467
});
68+
69+
70+
5571
res.cookie('username', req.user.username, {
5672
httpOnly: true,
5773
sameSite: 'none',

server/routers/passport-setup.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ passport.use(
3636
console.log('user is: ', currentUser);
3737
return done(null, currentUser);
3838
} else {
39+
const initials = profile.displayName.match(/\b\w/g).join('');
40+
const nums = profile.id.slice(0, 5);
3941
user
4042
.create({
41-
username: profile.displayName + '(Github)',
43+
username: initials + nums + '(Github)',
4244
githubId: profile.id
4345
})
4446
.then((data) => {
@@ -73,9 +75,11 @@ passport.use(
7375
console.log('user is: ', currentUser);
7476
return done(null, currentUser);
7577
} else {
78+
const initials = profile.displayName.match(/\b\w/g).join('');
79+
const nums = profile.id.slice(0, 5);
7680
user
7781
.create({
78-
username: profile.displayName + '(Google)',
82+
username: initials + nums + '(Google)',
7983
googleId: profile.id
8084
})
8185
.then((data) => {

server/server.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ app.post(
172172
(req, res) => res.status(200).json({ sessionId: res.locals.ssid })
173173
);
174174

175+
//confirming whether user is logged in for index.tsx rendering
176+
app.get(
177+
'/loggedIn',
178+
sessionController.isLoggedIn,
179+
(req, res) => res.status(200).json(res.locals.loggedIn)
180+
)
181+
182+
app.get('/logout', (req,res) => {
183+
184+
req.logout();
185+
res.redirect('/login');
186+
})
187+
175188
// user must be logged in to get or save projects, otherwise they will be redirected to login page
176189
app.post(
177190
'/saveProject',
@@ -180,12 +193,6 @@ app.post(
180193
(req, res) => res.status(200).json(res.locals.savedProject)
181194
);
182195

183-
//confirming whether user is logged in for index.tsx rendering
184-
app.get(
185-
'/loggedIn',
186-
sessionController.isLoggedIn,
187-
(req, res) => res.status(200).json(res.locals.loggedIn)
188-
)
189196

190197
app.post(
191198
'/getProjects',

0 commit comments

Comments
 (0)