Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add shouldReturnURI to configuration #723

Merged
merged 15 commits into from
Dec 13, 2024
21 changes: 13 additions & 8 deletions packages/@magic-ext/oauth2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class OAuthExtension extends Extension.Internal<'oauth2'> {
}

public loginWithRedirect(configuration: OAuthRedirectConfiguration) {
mattupham marked this conversation as resolved.
Show resolved Hide resolved
return this.utils.createPromiEvent<void>(async (resolve, reject) => {
return this.utils.createPromiEvent<null | string>(async (resolve, reject) => {
const parseRedirectResult = this.utils.createJsonRpcRequestPayload(OAuthPayloadMethods.Start, [
{
...configuration,
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/@magic-ext/oauth2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type OpenIDConnectUserInfo = OpenIDConnectProfile &
export interface OAuthRedirectStartResult {
oauthAuthoriationURI?: string;
useMagicServerCallback?: boolean;
shouldReturnURI?: boolean;
}

export interface OAuthRedirectResult {
Expand Down