diff --git a/packages/ui/src/apis/continue.test.ts b/packages/ui/src/apis/continue.test.ts new file mode 100644 index 000000000..02653081a --- /dev/null +++ b/packages/ui/src/apis/continue.test.ts @@ -0,0 +1,69 @@ +import ky from 'ky'; + +import { + continueWithPassword, + continueWithUsername, + continueWithEmail, + continueWithPhone, +} from './continue'; + +jest.mock('ky', () => ({ + extend: () => ky, + post: jest.fn(() => ({ + json: jest.fn(), + })), +})); + +describe('continue API', () => { + const mockKyPost = ky.post as jest.Mock; + + beforeEach(() => { + mockKyPost.mockReturnValueOnce({ + json: () => ({ + redirectTo: '/', + }), + }); + }); + + afterEach(() => { + mockKyPost.mockClear(); + }); + + it('continue with password', async () => { + await continueWithPassword('password'); + expect(ky.post).toBeCalledWith('/api/session/continue/password', { + json: { + password: 'password', + }, + }); + }); + + it('continue with username', async () => { + await continueWithUsername('username'); + expect(ky.post).toBeCalledWith('/api/session/continue/username', { + json: { + username: 'username', + }, + }); + }); + + it('continue with email', async () => { + await continueWithEmail('email'); + + expect(ky.post).toBeCalledWith('/api/session/continue/email', { + json: { + email: 'email', + }, + }); + }); + + it('continue with phone', async () => { + await continueWithPhone('phone'); + + expect(ky.post).toBeCalledWith('/api/session/continue/sms', { + json: { + phone: 'phone', + }, + }); + }); +}); diff --git a/packages/ui/src/apis/continue.ts b/packages/ui/src/apis/continue.ts new file mode 100644 index 000000000..8a4c385fc --- /dev/null +++ b/packages/ui/src/apis/continue.ts @@ -0,0 +1,32 @@ +import api from './api'; +import { bindSocialAccount } from './social'; + +type Response = { + redirectTo: string; +}; + +const continueApiPrefix = '/api/session/continue'; + +// Only bind with social after the sign-in bind password flow +export const continueWithPassword = async (password: string, socialToBind?: string) => { + const result = await api + .post(`${continueApiPrefix}/password`, { + json: { password }, + }) + .json(); + + if (result.redirectTo && socialToBind) { + await bindSocialAccount(socialToBind); + } + + return result; +}; + +export const continueWithUsername = async (username: string) => + api.post(`${continueApiPrefix}/username`, { json: { username } }).json(); + +export const continueWithEmail = async (email: string) => + api.post(`${continueApiPrefix}/email`, { json: { email } }).json(); + +export const continueWithPhone = async (phone: string) => + api.post(`${continueApiPrefix}/sms`, { json: { phone } }).json();