From 615db47493a29980da6dd221e614f520e67f7491 Mon Sep 17 00:00:00 2001 From: sherzod-bakhodirov Date: Fri, 8 Nov 2024 16:49:35 +0500 Subject: [PATCH 1/2] feat: implement transition from recover account to update email flow events --- .../@magic-sdk/provider/src/modules/user.ts | 20 ++++++- .../spec/modules/user/recoverAccount.spec.ts | 56 +++++++++++++++++++ .../types/src/modules/user-types.ts | 4 ++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/packages/@magic-sdk/provider/src/modules/user.ts b/packages/@magic-sdk/provider/src/modules/user.ts index 9aca28a57..eed0cef68 100644 --- a/packages/@magic-sdk/provider/src/modules/user.ts +++ b/packages/@magic-sdk/provider/src/modules/user.ts @@ -18,6 +18,8 @@ import { RecoveryFactorEventEmit, RecoverAccountEventHandlers, RecoverAccountEventEmit, + UpdateEmailEventHandlers, + UpdateEmailEventEmit, } from '@magic-sdk/types'; import { getItem, setItem, removeItem } from '../util/storage'; import { BaseModule } from './base-module'; @@ -150,7 +152,9 @@ export class UserModule extends BaseModule { this.sdk.testMode ? MagicPayloadMethod.RecoverAccountTestMode : MagicPayloadMethod.RecoverAccount, [{ email, showUI }], ); - const handle = this.request(requestPayload); + const handle = this.request( + requestPayload, + ); if (!showUI && handle) { handle.on(RecoverAccountEventEmit.Cancel, () => { @@ -162,6 +166,20 @@ export class UserModule extends BaseModule { handle.on(RecoverAccountEventEmit.VerifyOtp, (otp: string) => { this.createIntermediaryEvent(RecoverAccountEventEmit.VerifyOtp, requestPayload.id as string)(otp); }); + handle.on(RecoverAccountEventEmit.UpdateEmail, (newEmail: string) => { + this.createIntermediaryEvent(RecoverAccountEventEmit.UpdateEmail, requestPayload.id as string)(newEmail); + }); + + handle.on(UpdateEmailEventEmit.Cancel, () => { + this.createIntermediaryEvent(UpdateEmailEventEmit.Cancel, requestPayload.id as string)(); + }); + handle.on(UpdateEmailEventEmit.RetryWithNewEmail, (newEmail?) => { + this.createIntermediaryEvent(UpdateEmailEventEmit.RetryWithNewEmail, requestPayload.id as string)(newEmail); + }); + + handle.on(UpdateEmailEventEmit.VerifyEmailOtp, (otp: string) => { + this.createIntermediaryEvent(UpdateEmailEventEmit.VerifyEmailOtp, requestPayload.id as string)(otp); + }); } return handle; diff --git a/packages/@magic-sdk/provider/test/spec/modules/user/recoverAccount.spec.ts b/packages/@magic-sdk/provider/test/spec/modules/user/recoverAccount.spec.ts index 19713099b..1517c2a7d 100644 --- a/packages/@magic-sdk/provider/test/spec/modules/user/recoverAccount.spec.ts +++ b/packages/@magic-sdk/provider/test/spec/modules/user/recoverAccount.spec.ts @@ -76,3 +76,59 @@ test('method should create intermediary event on VerifyOtp', () => { expect(verifyEvent).toBe('verify-otp-code'); }); + +test('method should create intermediary event on UpdateEmail', () => { + const magic = createMagicSDK(); + magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {})); + const createIntermediaryEventFn = jest.fn(); + magic.user.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn); + + const handle = magic.user.recoverAccount({ email: 'test', showUI: false }); + handle.emit('update-email'); + + const UpdateEmail = magic.user.createIntermediaryEvent.mock.calls[0][0]; + + expect(UpdateEmail).toBe('update-email'); +}); + +test('method should create intermediary event on UpdateEmailEventEmit.Cancel', () => { + const magic = createMagicSDK(); + magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {})); + const createIntermediaryEventFn = jest.fn(); + magic.user.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn); + + const handle = magic.user.recoverAccount({ email: 'test', showUI: false }); + handle.emit('UpdateEmail/new-email-verification-cancel'); + + const cancelEvent = magic.user.createIntermediaryEvent.mock.calls[0][0]; + + expect(cancelEvent).toBe('UpdateEmail/new-email-verification-cancel'); +}); + +test('method should create intermediary event on RetryWithNewEmail', () => { + const magic = createMagicSDK(); + magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {})); + const createIntermediaryEventFn = jest.fn(); + magic.user.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn); + + const handle = magic.user.recoverAccount({ email: 'test', showUI: false }); + handle.emit('UpdateEmail/retry-with-new-email'); + + const retryEvent = magic.user.createIntermediaryEvent.mock.calls[0][0]; + + expect(retryEvent).toBe('UpdateEmail/retry-with-new-email'); +}); + +test('method should create intermediary event on VerifyEmailOtp', () => { + const magic = createMagicSDK(); + magic.user.overlay.post = jest.fn().mockImplementation(() => new Promise(() => {})); + const createIntermediaryEventFn = jest.fn(); + magic.user.createIntermediaryEvent = jest.fn().mockImplementation(() => createIntermediaryEventFn); + + const handle = magic.user.recoverAccount({ email: 'test', showUI: false }); + handle.emit('UpdateEmail/new-email-verify-otp'); + + const retryEvent = magic.user.createIntermediaryEvent.mock.calls[0][0]; + + expect(retryEvent).toBe('UpdateEmail/new-email-verify-otp'); +}); diff --git a/packages/@magic-sdk/types/src/modules/user-types.ts b/packages/@magic-sdk/types/src/modules/user-types.ts index 66a0840f4..078e5f374 100644 --- a/packages/@magic-sdk/types/src/modules/user-types.ts +++ b/packages/@magic-sdk/types/src/modules/user-types.ts @@ -120,12 +120,14 @@ export enum RecoverAccountEventOnReceived { LoginThrottled = 'login-throttled', InvalidSmsOtp = 'invalid-sms-otp', SmsVerified = 'sms-verified', + AccountRecovered = 'account-recovered', } export enum RecoverAccountEventEmit { Cancel = 'cancel', VerifyOtp = 'verify-otp-code', ResendSms = 'resend-sms-otp', + UpdateEmail = 'update-email', } export type RecoverAccountEventHandlers = { @@ -133,6 +135,7 @@ export type RecoverAccountEventHandlers = { [RecoverAccountEventEmit.Cancel]: () => void; [RecoverAccountEventEmit.VerifyOtp]: (otp: string) => void; [RecoverAccountEventEmit.ResendSms]: () => void; + [RecoverAccountEventEmit.UpdateEmail]: (email: string) => void; // Event sent [RecoverAccountEventOnReceived.SmsOtpSent]: ({ phoneNumber }: { phoneNumber: string }) => void; @@ -145,4 +148,5 @@ export type RecoverAccountEventHandlers = { errorCode: string; }) => {}; [RecoverAccountEventOnReceived.SmsVerified]: () => {}; + [RecoverAccountEventOnReceived.AccountRecovered]: () => {}; }; From 80795e61b3d4cefd344f4d2e26b29e4fd084639b Mon Sep 17 00:00:00 2001 From: sherzod-bakhodirov Date: Mon, 11 Nov 2024 21:37:15 +0500 Subject: [PATCH 2/2] feat: add update-email-required event to recover account --- packages/@magic-sdk/types/src/modules/user-types.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/@magic-sdk/types/src/modules/user-types.ts b/packages/@magic-sdk/types/src/modules/user-types.ts index 078e5f374..0294edf3f 100644 --- a/packages/@magic-sdk/types/src/modules/user-types.ts +++ b/packages/@magic-sdk/types/src/modules/user-types.ts @@ -121,6 +121,7 @@ export enum RecoverAccountEventOnReceived { InvalidSmsOtp = 'invalid-sms-otp', SmsVerified = 'sms-verified', AccountRecovered = 'account-recovered', + UpdateEmailRequired = 'update-email-required', } export enum RecoverAccountEventEmit { @@ -149,4 +150,5 @@ export type RecoverAccountEventHandlers = { }) => {}; [RecoverAccountEventOnReceived.SmsVerified]: () => {}; [RecoverAccountEventOnReceived.AccountRecovered]: () => {}; + [RecoverAccountEventOnReceived.UpdateEmailRequired]: () => {}; };