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:
commit
121c7ba736
2 changed files with 101 additions and 0 deletions
69
packages/ui/src/apis/continue.test.ts
Normal file
69
packages/ui/src/apis/continue.test.ts
Normal 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',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
32
packages/ui/src/apis/continue.ts
Normal file
32
packages/ui/src/apis/continue.ts
Normal 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>();
|
Loading…
Add table
Reference in a new issue