diff --git a/packages/@magic-ext/oauth2/src/index.ts b/packages/@magic-ext/oauth2/src/index.ts index d52f1c81..4f5b9473 100644 --- a/packages/@magic-ext/oauth2/src/index.ts +++ b/packages/@magic-ext/oauth2/src/index.ts @@ -35,7 +35,7 @@ export class OAuthExtension extends Extension.Internal<'oauth2'> { } public loginWithRedirect(configuration: OAuthRedirectConfiguration) { - return this.utils.createPromiEvent(async (resolve, reject) => { + return this.utils.createPromiEvent(async (resolve, reject) => { const parseRedirectResult = this.utils.createJsonRpcRequestPayload(OAuthPayloadMethods.Start, [ { ...configuration, @@ -58,18 +58,23 @@ export class OAuthExtension extends Extension.Internal<'oauth2'> { } if (successResult?.oauthAuthoriationURI) { - window.location.href = successResult.useMagicServerCallback + const redirectURI = successResult.useMagicServerCallback ? // @ts-ignore - this.sdk.endpoint is marked protected but we need to access it. new URL(successResult.oauthAuthoriationURI, this.sdk.endpoint).href : successResult.oauthAuthoriationURI; - } - resolve(); + if (successResult?.shouldReturnURI) { + resolve(redirectURI); + } else { + window.location.href = redirectURI; + } + } + resolve(null); }); } - public getRedirectResult(lifespan?: number) { - const queryString = window.location.search; + public getRedirectResult(lifespan?: number, optionalQueryString?: string) { + const queryString = optionalQueryString || window.location.search; // Remove the query from the redirect callback as a precaution to prevent // malicious parties from parsing it before we have a chance to use it. @@ -113,8 +118,8 @@ export class OAuthExtension extends Extension.Internal<'oauth2'> { console.log('Error while verifying telegram data', verificationError); } } - } catch (seamlessLoginError) { - console.log('Error while loading telegram-web-app script', seamlessLoginError); + } catch (seamlessLoginError) { + console.log('Error while loading telegram-web-app script', seamlessLoginError); } } } diff --git a/packages/@magic-ext/oauth2/src/types.ts b/packages/@magic-ext/oauth2/src/types.ts index ade8f4d5..7bf18fa6 100644 --- a/packages/@magic-ext/oauth2/src/types.ts +++ b/packages/@magic-ext/oauth2/src/types.ts @@ -73,6 +73,7 @@ export type OpenIDConnectUserInfo = OpenIDConnectProfile & export interface OAuthRedirectStartResult { oauthAuthoriationURI?: string; useMagicServerCallback?: boolean; + shouldReturnURI?: boolean; } export interface OAuthRedirectResult {