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

Enable SMS login #223

Merged
merged 22 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5b6ebc8
add sms api method
dgerrellsMagic Jul 30, 2021
ce07536
add sms api method
dgerrellsMagic Jul 30, 2021
607c06b
Merge branch 'dgerrells-ch42053-sms-feature' of github.com:magiclabs/…
dgerrellsMagic Aug 31, 2021
8602b3b
add unit test for login with sms
dgerrellsMagic Aug 31, 2021
5fb8a52
add release tag
dgerrellsMagic Aug 31, 2021
9b95020
add sms api method
dgerrellsMagic Jul 30, 2021
2f20e8b
add unit test for login with sms
dgerrellsMagic Aug 31, 2021
9aa00a4
add release tag
dgerrellsMagic Aug 31, 2021
c5aa669
Merge branch 'dgerrells-ch42053-sms-feature' of github.com:magiclabs/…
dgerrellsMagic Sep 14, 2021
3dd528c
add sms api method
dgerrellsMagic Jul 30, 2021
3dbf3ef
add unit test for login with sms
dgerrellsMagic Aug 31, 2021
eed0fa3
add release tag
dgerrellsMagic Aug 31, 2021
cea12c0
Merge branch 'dgerrells-ch42053-sms-feature' of github.com:magiclabs/…
dgerrellsMagic Sep 17, 2021
d71e3e5
update yarn lock file
dgerrellsMagic Sep 17, 2021
b44a6df
add sms api method
dgerrellsMagic Jul 30, 2021
b3ec51d
add unit test for login with sms
dgerrellsMagic Aug 31, 2021
5d8387c
add release tag
dgerrellsMagic Aug 31, 2021
f049e42
update yarn lock file
dgerrellsMagic Sep 17, 2021
9518b6e
update lock to yarn v2
dgerrellsMagic Sep 17, 2021
7d86e15
Merge branch 'dgerrells-ch42053-sms-feature' of github.com:magiclabs/…
dgerrellsMagic Sep 17, 2021
8eca712
remove prerelease tags
dgerrellsMagic Oct 1, 2021
13d6a62
Merge branch 'master' into dgerrells-ch42053-sms-feature
dgerrellsMagic Oct 1, 2021
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
16 changes: 15 additions & 1 deletion packages/@magic-sdk/provider/src/modules/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MagicPayloadMethod, LoginWithMagicLinkConfiguration } from '@magic-sdk/types';
import { MagicPayloadMethod, LoginWithMagicLinkConfiguration, LoginWithSmsConfiguration } from '@magic-sdk/types';
import { BaseModule } from './base-module';
import { createJsonRpcRequestPayload } from '../core/json-rpc';
import { SDKEnvironment } from '../core/sdk-environment';
Expand All @@ -25,6 +25,20 @@ export class AuthModule extends BaseModule {
return this.request<string | null, LoginWithMagicLinkEvents>(requestPayload);
}

/**
* Initiate an SMS login flow for a user. If successful,
* this method will return a Decenteralized ID token (with a default lifespan
* of 15 minutes)
*/
public loginWithSMS(configuration: LoginWithSmsConfiguration) {
const { phoneNumber } = configuration;
const requestPayload = createJsonRpcRequestPayload(
this.sdk.testMode ? MagicPayloadMethod.LoginWithSmsTestMode : MagicPayloadMethod.LoginWithSms,
[{ phoneNumber, showUI: true }],
);
return this.request<string | null>(requestPayload);
}

/**
* Log a user in with a special one-time-use credential token. This is
* currently used during magic link flows with a configured redirect to
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable global-require, @typescript-eslint/no-var-requires */

import browserEnv from '@ikscodes/browser-env';
import { MagicPayloadMethod } from '@magic-sdk/types';
import { isPromiEvent } from '../../../../src/util';
import { createMagicSDK, createMagicSDKTestMode } from '../../../factories';

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

const expectedPhoneNumber = 'hey hey I am a number but jk';

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

await magic.auth.loginWithSMS({ phoneNumber: expectedPhoneNumber });

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

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

await magic.auth.loginWithSMS({ phoneNumber: expectedPhoneNumber });

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

test('method should return a PromiEvent', () => {
const magic = createMagicSDK();
expect(isPromiEvent(magic.auth.loginWithSMS({ email: 'blag' }))).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 @@ -40,6 +40,7 @@ export interface JsonRpcResponsePayload<ResultType = any> {
* relayer.
*/
export enum MagicPayloadMethod {
LoginWithSms = 'magic_auth_login_with_sms',
LoginWithMagicLink = 'magic_auth_login_with_magic_link',
LoginWithCredential = 'magic_auth_login_with_credential',
GetIdToken = 'magic_auth_get_id_token',
Expand All @@ -48,6 +49,7 @@ export enum MagicPayloadMethod {
IsLoggedIn = 'magic_auth_is_logged_in',
Logout = 'magic_auth_logout',
UpdateEmail = 'magic_auth_update_email',
LoginWithSmsTestMode = 'magic_auth_login_with_sms_testing_mode',
LoginWithMagicLinkTestMode = 'magic_login_with_magic_link_testing_mode',
LoginWithCredentialTestMode = 'magic_auth_login_with_credential_testing_mode',
GetIdTokenTestMode = 'magic_auth_get_id_token_testing_mode',
Expand Down
4 changes: 4 additions & 0 deletions packages/@magic-sdk/types/src/modules/auth-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ export interface LoginWithMagicLinkConfiguration {
*/
redirectURI?: string;
}

export interface LoginWithSmsConfiguration {
phoneNumber: string;
}
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4118,14 +4118,14 @@ __metadata:
languageName: node
linkType: hard

"@magic-ext/oauth@^0.9.1, @magic-ext/oauth@workspace:packages/@magic-ext/oauth":
"@magic-ext/oauth@^0.9.2, @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": ^5.0.3
"@types/crypto-js": ~3.1.47
crypto-js: ^3.3.0
magic-sdk: ^6.0.6
magic-sdk: ^6.0.7
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -4168,8 +4168,8 @@ __metadata:
"@babel/core": ^7.9.6
"@babel/plugin-proposal-optional-chaining": ^7.9.0
"@babel/runtime": ^7.9.6
"@magic-ext/oauth": ^0.9.1
magic-sdk: ^6.0.6
"@magic-ext/oauth": ^0.9.2
magic-sdk: ^6.0.7
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -13895,7 +13895,7 @@ fsevents@^1.2.7:
languageName: unknown
linkType: soft

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