Skip to content

Commit

Permalink
Merge pull request #833 from magiclabs/PDEEXP-1544-Implement-magic_au…
Browse files Browse the repository at this point in the history
…th_recover_account_provider

feat: implement transition from recover account to update email flow …
  • Loading branch information
Ethella authored Nov 14, 2024
2 parents e7f0321 + 80795e6 commit a4d87e1
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/@magic-sdk/provider/src/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -150,7 +152,9 @@ export class UserModule extends BaseModule {
this.sdk.testMode ? MagicPayloadMethod.RecoverAccountTestMode : MagicPayloadMethod.RecoverAccount,
[{ email, showUI }],
);
const handle = this.request<string | boolean | null, RecoverAccountEventHandlers>(requestPayload);
const handle = this.request<string | boolean | null, RecoverAccountEventHandlers & UpdateEmailEventHandlers>(
requestPayload,
);

if (!showUI && handle) {
handle.on(RecoverAccountEventEmit.Cancel, () => {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
6 changes: 6 additions & 0 deletions packages/@magic-sdk/types/src/modules/user-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,23 @@ export enum RecoverAccountEventOnReceived {
LoginThrottled = 'login-throttled',
InvalidSmsOtp = 'invalid-sms-otp',
SmsVerified = 'sms-verified',
AccountRecovered = 'account-recovered',
UpdateEmailRequired = 'update-email-required',
}

export enum RecoverAccountEventEmit {
Cancel = 'cancel',
VerifyOtp = 'verify-otp-code',
ResendSms = 'resend-sms-otp',
UpdateEmail = 'update-email',
}

export type RecoverAccountEventHandlers = {
// Event Received
[RecoverAccountEventEmit.Cancel]: () => void;
[RecoverAccountEventEmit.VerifyOtp]: (otp: string) => void;
[RecoverAccountEventEmit.ResendSms]: () => void;
[RecoverAccountEventEmit.UpdateEmail]: (email: string) => void;

// Event sent
[RecoverAccountEventOnReceived.SmsOtpSent]: ({ phoneNumber }: { phoneNumber: string }) => void;
Expand All @@ -145,4 +149,6 @@ export type RecoverAccountEventHandlers = {
errorCode: string;
}) => {};
[RecoverAccountEventOnReceived.SmsVerified]: () => {};
[RecoverAccountEventOnReceived.AccountRecovered]: () => {};
[RecoverAccountEventOnReceived.UpdateEmailRequired]: () => {};
};

0 comments on commit a4d87e1

Please sign in to comment.