@@ -24,6 +24,60 @@ const {
24
24
25
25
let connection ;
26
26
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
+
27
81
const createExpressApp = ( ) => {
28
82
const expressApp = express ( ) ;
29
83
expressApp . use ( addRequestIdMiddleware ) ;
@@ -73,59 +127,12 @@ const createExpressApp = () => {
73
127
74
128
// Github authentication
75
129
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
- }
97
130
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' ) ) ;
109
133
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' ) ) ;
129
136
130
137
// Google authentication
131
138
// get current logged in user data from req.user object
@@ -225,54 +232,6 @@ const createExpressApp = () => {
225
232
passport . authenticate ( 'google' , { scope : [ 'profile' , 'email' ] } )
226
233
) ;
227
234
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
-
276
235
defineRoutes ( expressApp ) ;
277
236
defineErrorHandlingMiddleware ( expressApp ) ;
278
237
return expressApp ;
0 commit comments