0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-27 21:39:16 -05:00

Merge pull request #2346 from logto-io/simeng-log-4558-api-continue

feat(ui): add continue related apis
This commit is contained in:
simeng-li 2022-11-08 15:26:23 +08:00 committed by GitHub
commit 121c7ba736
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 0 deletions

View file

@ -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',
},
});
});
});

View file

@ -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<Response>();
if (result.redirectTo && socialToBind) {
await bindSocialAccount(socialToBind);
}
return result;
};
export const continueWithUsername = async (username: string) =>
api.post(`${continueApiPrefix}/username`, { json: { username } }).json<Response>();
export const continueWithEmail = async (email: string) =>
api.post(`${continueApiPrefix}/email`, { json: { email } }).json<Response>();
export const continueWithPhone = async (phone: string) =>
api.post(`${continueApiPrefix}/sms`, { json: { phone } }).json<Response>();