@@ -9,6 +9,7 @@ import { Button } from "@betterinternship/components";
99import { Card } from "@/components/ui/card" ;
1010import { Loader } from "@/components/ui/loader" ;
1111import { DiscordService } from "@/lib/api/discord.api" ;
12+ import { toDiscordAppAuthorizationUrl } from "@/lib/discord/mobile-oauth" ;
1213import { JobService , UserService } from "@/lib/api/services" ;
1314import { useAuthContext } from "@/lib/ctx-auth" ;
1415import useModalRegistry from "@/components/modals/modal-registry" ;
@@ -92,8 +93,33 @@ function DiscordSetupContent() {
9293 void apply ( ) ;
9394 } , [ resumeReady , status . data ?. linked ] ) ;
9495
95- const connect = ( ) => {
96- window . location . assign ( DiscordService . authorizationUrl ( jobId ) ) ;
96+ const connect = async ( ) => {
97+ const isMobile = / A n d r o i d | i P h o n e | i P a d | i P o d / i. test ( navigator . userAgent ) ;
98+ if ( ! isMobile ) {
99+ window . location . assign ( DiscordService . authorizationUrl ( jobId ) ) ;
100+ return ;
101+ }
102+
103+ try {
104+ const result = await DiscordService . getMobileAuthorizationUrl ( jobId ) ;
105+ if ( ! result . success || ! result . authorizationUrl ) {
106+ throw new Error ( result . message || "Could not start Discord connection." ) ;
107+ }
108+
109+ const webAuthorizationUrl = result . authorizationUrl ;
110+ const fallback = window . setTimeout ( ( ) => {
111+ window . location . assign ( webAuthorizationUrl ) ;
112+ } , 1500 ) ;
113+
114+ window . addEventListener ( "pagehide" , ( ) => window . clearTimeout ( fallback ) , {
115+ once : true ,
116+ } ) ;
117+ window . location . assign ( toDiscordAppAuthorizationUrl ( webAuthorizationUrl ) ) ;
118+ } catch {
119+ // The regular OAuth page is the supported fallback when the app cannot
120+ // be opened or a device blocks custom URL schemes.
121+ window . location . assign ( DiscordService . authorizationUrl ( jobId ) ) ;
122+ }
97123 } ;
98124
99125 const setDefaultResume = async ( resumeId : string ) => {
@@ -197,7 +223,7 @@ function DiscordSetupContent() {
197223 ) }
198224
199225 { ! status . data ?. linked && resumeReady && (
200- < Button className = "w-full" onClick = { connect } >
226+ < Button className = "w-full" onClick = { ( ) => void connect ( ) } >
201227 Connect Discord and Apply
202228 </ Button >
203229 ) }
0 commit comments