Skip to content

Commit 29f8865

Browse files
committed
refactor: consolidate auth callback handlers
- Extract duplicated authentication callback logic into reusable function - Create handleAuthCallback helper that works for both GitHub and Google strategies - Reduce code duplication while maintaining existing functionality - Keep strategy-specific logging for GitHub callbacks
1 parent 8fc869e commit 29f8865

File tree

1 file changed

+58
-99
lines changed

1 file changed

+58
-99
lines changed

src/server.js

Lines changed: 58 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,60 @@ const {
2424

2525
let connection;
2626

27+
const handleAuthCallback = (strategy) => {
28+
return [
29+
function (req, res, next) {
30+
passport.authenticate(
31+
strategy,
32+
{
33+
failureRedirect: `${config.CLIENT_HOST}/login`,
34+
},
35+
(err, user, info, status) => {
36+
if (err || !user) {
37+
logger.error('Failed to authenticate user', err);
38+
return res.redirect(
39+
`${config.CLIENT_HOST}/login?error=${err?.name}`
40+
);
41+
}
42+
req.logIn(user, function (err) {
43+
if (err) {
44+
return res.redirect(
45+
`${config.CLIENT_HOST}/login?error=failed-to-authenticate`
46+
);
47+
}
48+
49+
req.session.userId = user._id;
50+
req.session.sessionId = req.sessionID;
51+
req.session.save((err) => {
52+
if (err) {
53+
logger.error('Failed to save session', err);
54+
} else {
55+
logger.info('Session saved');
56+
}
57+
});
58+
59+
next();
60+
});
61+
}
62+
)(req, res, next);
63+
},
64+
function (req, res) {
65+
if (strategy === 'github') {
66+
logger.info('/api/auth/github/callback', {
67+
username: req.user.username,
68+
});
69+
}
70+
const userId = req.user._id.toString();
71+
res.cookie('userId', userId, {
72+
httpOnly: true,
73+
secure: true,
74+
sameSite: 'lax',
75+
});
76+
res.redirect(`${config.CLIENT_HOST}/login-success`);
77+
},
78+
];
79+
};
80+
2781
const createExpressApp = () => {
2882
const expressApp = express();
2983
expressApp.use(addRequestIdMiddleware);
@@ -73,59 +127,12 @@ const createExpressApp = () => {
73127

74128
// Github authentication
75129
expressApp.get('/api/auth/github', passport.authenticate('github'));
76-
expressApp.get(
77-
'/api/auth/github/callback',
78-
function (req, res, next) {
79-
passport.authenticate(
80-
'github',
81-
{
82-
failureRedirect: `${config.CLIENT_HOST}/login`,
83-
},
84-
(err, user, info, status) => {
85-
if (err || !user) {
86-
logger.error('Failed to authenticate user', err);
87-
return res.redirect(
88-
`${config.CLIENT_HOST}/login?error=${err?.name}`
89-
);
90-
}
91-
req.logIn(user, function (err) {
92-
if (err) {
93-
return res.redirect(
94-
`${config.CLIENT_HOST}/login?error=failed-to-authenticate`
95-
);
96-
}
97130

98-
// modify the session
99-
req.session.userId = user._id;
100-
req.session.sessionId = req.sessionID;
101-
// update the session
102-
req.session.save((err) => {
103-
if (err) {
104-
logger.error('Failed to save session', err);
105-
} else {
106-
logger.info('Session saved');
107-
}
108-
});
131+
// Replace the GitHub callback route with:
132+
expressApp.get('/api/auth/github/callback', ...handleAuthCallback('github'));
109133

110-
next();
111-
});
112-
}
113-
)(req, res, next);
114-
},
115-
function (req, res) {
116-
logger.info('/api/auth/github/callback', { username: req.user.username });
117-
// prepare the cookie here
118-
const userId = req.user._id.toString();
119-
120-
res.cookie('userId', userId, {
121-
httpOnly: true,
122-
secure: true, // Use secure in production (HTTPS)
123-
sameSite: 'lax', // Adjust depending on deployment
124-
});
125-
// Successful authentication, redirect home.
126-
res.redirect(`${config.CLIENT_HOST}/login-success`);
127-
}
128-
);
134+
// Replace the Google callback route with:
135+
expressApp.get('/api/auth/google/callback', ...handleAuthCallback('google'));
129136

130137
// Google authentication
131138
// get current logged in user data from req.user object
@@ -225,54 +232,6 @@ const createExpressApp = () => {
225232
passport.authenticate('google', { scope: ['profile', 'email'] })
226233
);
227234

228-
expressApp.get(
229-
'/api/auth/google/callback',
230-
function (req, res, next) {
231-
passport.authenticate(
232-
'google',
233-
{
234-
failureRedirect: `${config.CLIENT_HOST}/login`,
235-
},
236-
(err, user, info, status) => {
237-
if (err || !user) {
238-
logger.error('Failed to authenticate user', err);
239-
return res.redirect(
240-
`${config.CLIENT_HOST}/login?error=${err?.name}`
241-
);
242-
}
243-
req.logIn(user, function (err) {
244-
if (err) {
245-
return res.redirect(
246-
`${config.CLIENT_HOST}/login?error=failed-to-authenticate`
247-
);
248-
}
249-
250-
req.session.userId = user._id;
251-
req.session.sessionId = req.sessionID;
252-
req.session.save((err) => {
253-
if (err) {
254-
logger.error('Failed to save session', err);
255-
} else {
256-
logger.info('Session saved');
257-
}
258-
});
259-
260-
next();
261-
});
262-
}
263-
)(req, res, next);
264-
},
265-
function (req, res) {
266-
const userId = req.user._id.toString();
267-
res.cookie('userId', userId, {
268-
httpOnly: true,
269-
secure: true,
270-
sameSite: 'lax',
271-
});
272-
res.redirect(`${config.CLIENT_HOST}/login-success`);
273-
}
274-
);
275-
276235
defineRoutes(expressApp);
277236
defineErrorHandlingMiddleware(expressApp);
278237
return expressApp;

0 commit comments

Comments
 (0)