@@ -60,14 +60,26 @@ function clearBypassSession(req) {
6060 delete req . session . devBypassLoggedOut ;
6161}
6262
63+ function normalizeReturnTo ( value ) {
64+ if ( typeof value !== "string" ) return "/main" ;
65+ const trimmed = value . trim ( ) ;
66+ if ( ! trimmed || ! trimmed . startsWith ( "/" ) || trimmed . startsWith ( "//" ) ) {
67+ return "/main" ;
68+ }
69+ return trimmed ;
70+ }
71+
6372export function createAuthRouter ( ) {
6473 const router = express . Router ( ) ;
6574
6675 router . get ( "/hackclub/login" , oauthStartLimiter , ( req , res ) => {
76+ const returnTo = normalizeReturnTo ( req . query ?. returnTo ) ;
77+
6778 if ( shouldBypassAuth ( req ) ) {
6879 clearBypassSession ( req ) ;
6980 req . session . devBypassUser = getLocalBypassUser ( ) ;
70- res . redirect ( 302 , `${ getAppOrigin ( ) } /main` ) ;
81+ req . session . devBypassReturnTo = returnTo ;
82+ res . redirect ( 302 , `${ getAppOrigin ( ) } ${ returnTo } ` ) ;
7183 return ;
7284 }
7385
@@ -89,6 +101,7 @@ export function createAuthRouter() {
89101
90102 req . session . oauthState = state ;
91103 req . session . oauthRedirectUri = redirectUri ;
104+ req . session . oauthReturnTo = returnTo ;
92105
93106 const url = getAuthorizeUrl ( { state, redirectUri } ) ;
94107 res . redirect ( 302 , url ) ;
@@ -97,6 +110,7 @@ export function createAuthRouter() {
97110 router . get ( "/hackclub/callback" , oauthCallbackLimiter , async ( req , res ) => {
98111 const storedRedirectUri = req . session ?. oauthRedirectUri ;
99112 const sessionState = req . session ?. oauthState ;
113+ const returnTo = normalizeReturnTo ( req . session ?. oauthReturnTo ) ;
100114 const redirectUri =
101115 typeof storedRedirectUri === "string" && storedRedirectUri
102116 ? storedRedirectUri
@@ -105,6 +119,7 @@ export function createAuthRouter() {
105119 if ( req . session ) {
106120 delete req . session . oauthState ;
107121 delete req . session . oauthRedirectUri ;
122+ delete req . session . oauthReturnTo ;
108123 }
109124
110125 const appOrigin = redirectUri ? appOriginFromRedirectUri ( redirectUri ) : getAppOrigin ( ) ;
@@ -141,7 +156,7 @@ export function createAuthRouter() {
141156 req . session . userId = user . id ;
142157 req . session . hackclubSub = user . hackclub_sub ;
143158
144- res . redirect ( 302 , `${ appOrigin } /main ` ) ;
159+ res . redirect ( 302 , `${ appOrigin } ${ returnTo } ` ) ;
145160 } catch ( error ) {
146161 console . error ( "[auth] Hack Club callback failed:" , error ) ;
147162 res . redirect ( 302 , `${ getAppOrigin ( ) } /?error=oauth_callback` ) ;
0 commit comments