@@ -129,25 +129,33 @@ async function githubLogin(req: express.Request, res: express.Response) : Promis
129129 // If the user is returning from GitHub auth page with an auth code
130130 // we exchange it for an access token and store it in the session
131131 if ( typeof auth_code === "string" ) {
132- const { authentication } = await oauthApp . createToken ( { code : auth_code } ) ;
133- req . session . githubToken = authentication . token ;
134- req . session . save ( ( err ) => {
135- if ( err ) {
136- console . error ( "Error saving session:" , err ) ;
137- }
132+ try {
133+ const { authentication } = await oauthApp . createToken ( { code : auth_code } ) ;
134+ req . session . githubToken = authentication . token ;
135+ await new Promise ( ( resolve , reject ) => {
136+ req . session . save ( ( err ) => {
137+ if ( err ) {
138+ console . error ( "Error saving session:" , err ) ;
139+ reject ( err ) ;
140+ }
141+ resolve ( undefined ) ;
142+ } ) ;
143+ } ) ;
138144 // Redirect the user to the original URL without the auth code
139145 const redirectUri = req . originalUrl . split ( "?" ) [ 0 ] ;
140146 res . redirect ( redirectUri ) ;
141- res . end ( ) ;
142147 return undefined ;
143- } ) ;
148+ } catch ( error ) {
149+ console . error ( "OAuth callback error:" , error ) ;
150+ res . status ( 500 ) . send ( "Authentication failed." ) ;
151+ return undefined ;
152+ }
144153 }
145154
146155 // If the user is not authenticated, we redirect her to GitHub for authentication
147156 if ( ! auth_code && ! req . session . githubToken ) {
148157 const redirectUri = req . originalUrl ;
149158 res . redirect ( githubLoginUrl ( redirectUri ) ) ;
150- res . end ( ) ;
151159 return undefined ;
152160 }
153161
0 commit comments