Skip to content

Commit

Permalink
Merge pull request #414 from magiclabs/mushfichowdhury-login-with-ema…
Browse files Browse the repository at this point in the history
…il-otp-whitelabel

LoginWithEmailOtp Whitelabeling
  • Loading branch information
mushfichowdhury-magic authored Feb 28, 2023
2 parents 4e99004 + 6c8b0bc commit fea9d43
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
13 changes: 11 additions & 2 deletions packages/@magic-sdk/provider/src/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,20 @@ export class AuthModule extends BaseModule {
* of 15 minutes)
*/
public loginWithEmailOTP(configuration: LoginWithEmailOTPConfiguration) {
const { email } = configuration;
const { email, showUI } = configuration;
const requestPayload = createJsonRpcRequestPayload(
this.sdk.testMode ? MagicPayloadMethod.LoginWithEmailOTPTestMode : MagicPayloadMethod.LoginWithEmailOTP,
[{ email, showUI: true }],
[{ email, showUI }],
);
if (!showUI) {
const handle = this.request<string | null, LoginWithEmailOTPEvents>(requestPayload);
if (handle) {
handle.on('email-otp-sent', () => {
this.createIntermediaryEvent('verify-email-otp', requestPayload.id as any)('otp');
});
}
return handle;
}
return this.request<string | null, LoginWithEmailOTPEvents>(requestPayload);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,31 @@ test('Generates JSON RPC request payload with `email` parameter', async () => {
const magic = createMagicSDK();
magic.auth.request = jest.fn();

await magic.auth.loginWithEmailOTP({ email: expectedEmail });
await magic.auth.loginWithEmailOTP({ email: expectedEmail, showUI: true });

const requestPayload = magic.auth.request.mock.calls[0][0];
expect(requestPayload.jsonrpc).toBe('2.0');
expect(requestPayload.method).toBe(MagicPayloadMethod.LoginWithEmailOTP);
expect(requestPayload.params).toEqual([{ email: expectedEmail, showUI: true }]);
});

test('Generates JSON RPC request payload with `showUI: false` parameter', async () => {
const magic = createMagicSDK();
magic.auth.request = jest.fn();

await magic.auth.loginWithEmailOTP({ email: expectedEmail, showUI: false });

const requestPayload = magic.auth.request.mock.calls[0][0];
expect(requestPayload.jsonrpc).toBe('2.0');
expect(requestPayload.method).toBe(MagicPayloadMethod.LoginWithEmailOTP);
expect(requestPayload.params).toEqual([{ email: expectedEmail, showUI: false }]);
});

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

await magic.auth.loginWithEmailOTP({ email: expectedEmail });
await magic.auth.loginWithEmailOTP({ email: expectedEmail, showUI: true });

const requestPayload = magic.auth.request.mock.calls[0][0];
expect(requestPayload.jsonrpc).toBe('2.0');
Expand Down
5 changes: 1 addition & 4 deletions packages/@magic-sdk/types/src/modules/auth-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ export type LoginWithMagicLinkEvents = {
};

export type LoginWithEmailOTPEvents = {
'send-email-otp': (email: string) => void;
'email-otp-sent': () => void;
'verify-email-otp': (otp: string) => void;
'email-not-deliverable': () => void;
'invalid-email-format': () => void;
'invalid-email-otp': () => void;
'lockout-too-many-failed-attempts': () => void;
};
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2820,14 +2820,14 @@ __metadata:
languageName: unknown
linkType: soft

"@magic-ext/oauth@^7.4.0, @magic-ext/oauth@workspace:packages/@magic-ext/oauth":
"@magic-ext/oauth@^7.4.1, @magic-ext/oauth@workspace:packages/@magic-ext/oauth":
version: 0.0.0-use.local
resolution: "@magic-ext/oauth@workspace:packages/@magic-ext/oauth"
dependencies:
"@magic-sdk/types": ^11.4.0
"@types/crypto-js": ~3.1.47
crypto-js: ^3.3.0
magic-sdk: ^13.4.0
magic-sdk: ^13.4.1
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -2955,8 +2955,8 @@ __metadata:
"@babel/core": ^7.9.6
"@babel/plugin-proposal-optional-chaining": ^7.9.0
"@babel/runtime": ^7.9.6
"@magic-ext/oauth": ^7.4.0
magic-sdk: ^13.4.0
"@magic-ext/oauth": ^7.4.1
magic-sdk: ^13.4.1
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -7218,9 +7218,9 @@ __metadata:
linkType: hard

"electron-to-chromium@npm:^1.4.284":
version: 1.4.312
resolution: "electron-to-chromium@npm:1.4.312"
checksum: 6f76f7a92180715985f6008409f8cb21fdef4e63fcd80d88907c395afb5dad84d7cbc8e274f718ece8df9bdbdcf14e43d446f7bee2824131f21fe92b74df2c1b
version: 1.4.313
resolution: "electron-to-chromium@npm:1.4.313"
checksum: 694c379e0800e55e3e2966f73557bfc3bad66e1eb120928d33e8f0619b59ec057eb94ecb27a06e6f7ea1892565f6b8a87b03129a8243baa0d1ccea9579040a83
languageName: node
linkType: hard

Expand Down Expand Up @@ -12338,7 +12338,7 @@ fsevents@^2.3.2:
languageName: unknown
linkType: soft

"magic-sdk@^13.4.0, magic-sdk@workspace:packages/magic-sdk":
"magic-sdk@^13.4.1, magic-sdk@workspace:packages/magic-sdk":
version: 0.0.0-use.local
resolution: "magic-sdk@workspace:packages/magic-sdk"
dependencies:
Expand Down

0 comments on commit fea9d43

Please sign in to comment.