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 recoverAccount api #458

Merged
merged 5 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions packages/@magic-sdk/provider/src/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
MagicUserMetadata,
GenerateIdTokenConfiguration,
UpdateEmailConfiguration,
RecoverAccountConfiguration,
} from '@magic-sdk/types';
import { BaseModule } from './base-module';
import { createJsonRpcRequestPayload } from '../core/json-rpc';
Expand All @@ -17,7 +18,6 @@ type UpdateEmailEvents = {
retry: () => void;
};
export class UserModule extends BaseModule {
/** */
public getIdToken(configuration?: GetIdTokenConfiguration) {
const requestPayload = createJsonRpcRequestPayload(
this.sdk.testMode ? MagicPayloadMethod.GetIdTokenTestMode : MagicPayloadMethod.GetIdToken,
Expand All @@ -26,7 +26,6 @@ export class UserModule extends BaseModule {
return this.request<string>(requestPayload);
}

/** */
public generateIdToken(configuration?: GenerateIdTokenConfiguration) {
const requestPayload = createJsonRpcRequestPayload(
this.sdk.testMode ? MagicPayloadMethod.GenerateIdTokenTestMode : MagicPayloadMethod.GenerateIdToken,
Expand All @@ -35,15 +34,13 @@ export class UserModule extends BaseModule {
return this.request<string>(requestPayload);
}

/** */
public getMetadata() {
const requestPayload = createJsonRpcRequestPayload(
this.sdk.testMode ? MagicPayloadMethod.GetMetadataTestMode : MagicPayloadMethod.GetMetadata,
);
return this.request<MagicUserMetadata>(requestPayload);
}

/** */
public updateEmail(configuration: UpdateEmailConfiguration) {
const { email, showUI = true } = configuration;
const requestPayload = createJsonRpcRequestPayload(
Expand All @@ -53,15 +50,13 @@ export class UserModule extends BaseModule {
return this.request<string | null, UpdateEmailEvents>(requestPayload);
}

/** */
public isLoggedIn() {
const requestPayload = createJsonRpcRequestPayload(
this.sdk.testMode ? MagicPayloadMethod.IsLoggedInTestMode : MagicPayloadMethod.IsLoggedIn,
);
return this.request<boolean>(requestPayload);
}

/** */
public logout() {
clearKeys();
const requestPayload = createJsonRpcRequestPayload(
Expand All @@ -70,19 +65,25 @@ export class UserModule extends BaseModule {
return this.request<boolean>(requestPayload);
}

/** */
public showSettings() {
const requestPayload = createJsonRpcRequestPayload(
this.sdk.testMode ? MagicPayloadMethod.UserSettingsTestMode : MagicPayloadMethod.UserSettings,
);
return this.request<MagicUserMetadata>(requestPayload);
}

/** */
public updatePhoneNumber() {
const requestPayload = createJsonRpcRequestPayload(
this.sdk.testMode ? MagicPayloadMethod.UpdatePhoneNumberTestMode : MagicPayloadMethod.UpdatePhoneNumber,
);
return this.request<string | null>(requestPayload);
}

public recoverAccount(configuration: RecoverAccountConfiguration) {
const requestPayload = createJsonRpcRequestPayload(
this.sdk.testMode ? MagicPayloadMethod.RecoverAccountTestMode : MagicPayloadMethod.RecoverAccount,
[configuration],
);
return this.request<boolean | null>(requestPayload);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import browserEnv from '@ikscodes/browser-env';
import { createMagicSDK, createMagicSDKTestMode } from '../../../factories';
import { isPromiEvent } from '../../../../src';

beforeEach(() => {
browserEnv.restore();
});

test('Generate JSON RPC request payload with method `magic_auth_recover_account`', async () => {
const magic = createMagicSDK();
magic.user.request = jest.fn();

await magic.user.recoverAccount({ email: 'test' });

const requestPayload = magic.user.request.mock.calls[0][0];
expect(requestPayload.jsonrpc).toBe('2.0');
expect(requestPayload.method).toBe('magic_auth_recover_account');
expect(requestPayload.params).toEqual([{ email: 'test' }]);
});

test('If `testMode` is enabled, testing-specific RPC method is used', async () => {
const magic = createMagicSDKTestMode();
magic.user.request = jest.fn();

await magic.user.recoverAccount({ email: 'test' });

const requestPayload = magic.user.request.mock.calls[0][0];
expect(requestPayload.jsonrpc).toBe('2.0');
expect(requestPayload.method).toBe('magic_auth_recover_account_testing_mode');
expect(requestPayload.params).toEqual([{ email: 'test' }]);
});

test('method should return a PromiEvent', () => {
const magic = createMagicSDK();
expect(isPromiEvent(magic.user.recoverAccount({ email: 'test' }))).toBeTruthy();
});
2 changes: 2 additions & 0 deletions packages/@magic-sdk/types/src/core/json-rpc-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ export enum MagicPayloadMethod {
Disconnect = 'mc_disconnect',
UpdatePhoneNumber = 'magic_auth_update_phone_number',
UpdatePhoneNumberTestMode = 'magic_auth_update_phone_number_testing_mode',
RecoverAccount = 'magic_auth_recover_account',
RecoverAccountTestMode = 'magic_auth_recover_account_testing_mode',
}
7 changes: 7 additions & 0 deletions packages/@magic-sdk/types/src/modules/user-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ export interface UpdateWebAuthnInfoConfiguration {
*/
nickname: string;
}

export interface RecoverAccountConfiguration {
/**
* The email to recover
*/
email: string;
}