Skip to content

Commit

Permalink
Merge pull request #828 from magiclabs/PDEEXP-1954-OAuth-popup-flow-c…
Browse files Browse the repository at this point in the history
…onsolidation

feat: implement OAuth login with Popup
  • Loading branch information
Ethella authored Nov 16, 2024
2 parents 4de10ab + 16ab291 commit 7f56886
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/@magic-ext/oauth2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
OAuthRedirectConfiguration,
OAuthPayloadMethods,
OAuthRedirectStartResult,
OAuthPopupConfiguration,
} from './types';

export class OAuthExtension extends Extension.Internal<'oauth2'> {
Expand Down Expand Up @@ -64,6 +65,18 @@ export class OAuthExtension extends Extension.Internal<'oauth2'> {

return getResult.call(this, queryString, lifespan);
}

public loginWithPopup(configuration: OAuthPopupConfiguration) {
const requestPayload = this.utils.createJsonRpcRequestPayload(OAuthPayloadMethods.Popup, [
{
...configuration,
apiKey: this.sdk.apiKey,
platform: 'web',
},
]);

return this.request<OAuthRedirectResult | OAuthRedirectError>(requestPayload);
}
}

function getResult(this: OAuthExtension, queryString: string, lifespan?: number) {
Expand Down
7 changes: 7 additions & 0 deletions packages/@magic-ext/oauth2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MagicUserMetadata } from '@magic-sdk/types';
export enum OAuthPayloadMethods {
Start = 'magic_oauth_login_with_redirect_start',
Verify = 'magic_oauth_login_with_redirect_verify',
Popup = 'magic_oauth_login_with_popup',
}

export type OAuthProvider =
Expand Down Expand Up @@ -99,6 +100,12 @@ export interface OAuthRedirectConfiguration {
loginHint?: string;
}

export interface OAuthPopupConfiguration {
provider: OAuthProvider;
scope?: string[];
loginHint?: string;
}

export enum OAuthErrorCode {
InvalidRequest = 'invalid_request',
InvalidClient = 'invalid_client',
Expand Down
2 changes: 2 additions & 0 deletions packages/@magic-sdk/types/src/core/message-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export enum MagicIncomingWindowMessage {
MAGIC_MG_BOX_SEND_RECEIPT = 'MAGIC_MG_BOX_SEND_RECEIPT',
MAGIC_SEND_PRODUCT_ANNOUNCEMENT = 'MAGIC_SEND_PRODUCT_ANNOUNCEMENT',
MAGIC_PONG = 'MAGIC_PONG',
MAGIC_POPUP_RESPONSE = 'MAGIC_POPUP_RESPONSE',
MAGIC_POPUP_OAUTH_VERIFY_RESPONSE = 'MAGIC_POPUP_OAUTH_VERIFY_RESPONSE',
}

export enum MagicOutgoingWindowMessage {
Expand Down

0 comments on commit 7f56886

Please sign in to comment.