0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-31 22:51:25 -05:00

refactor(core): fix ui api calls and uts

This commit is contained in:
Darcy Ye 2022-09-29 12:04:04 +08:00
parent 365e381344
commit 996e31008a
No known key found for this signature in database
GPG key ID: B46F4C07EDEFC610
4 changed files with 79 additions and 59 deletions

View file

@ -131,9 +131,9 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
const { result } = await provider.interactionDetails(ctx.req, ctx.res);
console.log(result);
const passwordlessVerificationResult = verificationGuard.safeParse(result);
const verificationResult = verificationGuard.safeParse(result);
assertThat(
passwordlessVerificationResult.success,
verificationResult.success,
new RequestError({
code: 'session.verification_session_not_found',
status: 404,
@ -142,7 +142,7 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
const {
verification: { phone, flow, expiresAt },
} = passwordlessVerificationResult.data;
} = verificationResult.data;
const type = getPasswordlessRelatedLogType('sign-in', 'sms');
ctx.log(type, { phone, flow, expiresAt });
@ -173,9 +173,9 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
router.post(`${signInRoute}/email`, async (ctx, next) => {
const { result } = await provider.interactionDetails(ctx.req, ctx.res);
const passwordlessVerificationResult = verificationGuard.safeParse(result);
const verificationResult = verificationGuard.safeParse(result);
assertThat(
passwordlessVerificationResult.success,
verificationResult.success,
new RequestError({
code: 'session.verification_session_not_found',
status: 404,
@ -184,7 +184,7 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
const {
verification: { email, flow, expiresAt },
} = passwordlessVerificationResult.data;
} = verificationResult.data;
const type = getPasswordlessRelatedLogType('sign-in', 'email');
ctx.log(type, { email, flow, expiresAt });
@ -216,9 +216,9 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
router.post(`${registerRoute}/sms`, async (ctx, next) => {
const { result } = await provider.interactionDetails(ctx.req, ctx.res);
const passwordlessVerificationResult = verificationGuard.safeParse(result);
const verificationResult = verificationGuard.safeParse(result);
assertThat(
passwordlessVerificationResult.success,
verificationResult.success,
new RequestError({
code: 'session.verification_session_not_found',
status: 404,
@ -226,8 +226,8 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
);
const {
verification: { email, phone, flow, expiresAt },
} = passwordlessVerificationResult.data;
verification: { phone, flow, expiresAt },
} = verificationResult.data;
const type = getPasswordlessRelatedLogType('register', 'sms');
ctx.log(type, { phone, flow, expiresAt });
@ -259,9 +259,9 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
router.post(`${registerRoute}/email`, async (ctx, next) => {
const { result } = await provider.interactionDetails(ctx.req, ctx.res);
const passwordlessVerificationResult = verificationGuard.safeParse(result);
const verificationResult = verificationGuard.safeParse(result);
assertThat(
passwordlessVerificationResult.success,
verificationResult.success,
new RequestError({
code: 'session.verification_session_not_found',
status: 404,
@ -270,7 +270,7 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
const {
verification: { email, flow, expiresAt },
} = passwordlessVerificationResult.data;
} = verificationResult.data;
const type = getPasswordlessRelatedLogType('register', 'email');
ctx.log(type, { email, flow, expiresAt });

View file

@ -87,9 +87,10 @@ describe('api', () => {
it('sendSignInSmsPasscode', async () => {
await sendSignInSmsPasscode(phone);
expect(ky.post).toBeCalledWith('/api/session/sign-in/passwordless/sms/send-passcode', {
expect(ky.post).toBeCalledWith('/api/session/passwordless/sms/send', {
json: {
phone,
flow: 'sign-in',
},
});
});
@ -101,19 +102,22 @@ describe('api', () => {
}),
});
await verifySignInSmsPasscode(phone, code);
expect(ky.post).toBeCalledWith('/api/session/sign-in/passwordless/sms/verify-passcode', {
expect(ky.post).toBeCalledWith('/api/session/passwordless/sms/verify', {
json: {
phone,
code,
flow: 'sign-in',
},
});
expect(ky.post).toBeCalledWith('/api/session/sign-in/passwordless/sms');
});
it('sendSignInEmailPasscode', async () => {
await sendSignInEmailPasscode(email);
expect(ky.post).toBeCalledWith('/api/session/sign-in/passwordless/email/send-passcode', {
expect(ky.post).toBeCalledWith('/api/session/passwordless/email/send', {
json: {
email,
flow: 'sign-in',
},
});
});
@ -125,12 +129,14 @@ describe('api', () => {
}),
});
await verifySignInEmailPasscode(email, code);
expect(ky.post).toBeCalledWith('/api/session/sign-in/passwordless/email/verify-passcode', {
expect(ky.post).toBeCalledWith('/api/session/passwordless/email/verify', {
json: {
email,
code,
flow: 'sign-in',
},
});
expect(ky.post).toBeCalledWith('/api/session/sign-in/passwordless/email');
});
it('consent', async () => {
@ -150,40 +156,46 @@ describe('api', () => {
it('sendRegisterSmsPasscode', async () => {
await sendRegisterSmsPasscode(phone);
expect(ky.post).toBeCalledWith('/api/session/register/passwordless/sms/send-passcode', {
expect(ky.post).toBeCalledWith('/api/session/passwordless/sms/send', {
json: {
phone,
flow: 'register',
},
});
});
it('verifyRegisterSmsPasscode', async () => {
await verifyRegisterSmsPasscode(phone, code);
expect(ky.post).toBeCalledWith('/api/session/register/passwordless/sms/verify-passcode', {
expect(ky.post).toBeCalledWith('/api/session/passwordless/sms/verify', {
json: {
phone,
code,
flow: 'register',
},
});
expect(ky.post).toBeCalledWith('/api/session/register/passwordless/sms');
});
it('sendRegisterEmailPasscode', async () => {
await sendRegisterEmailPasscode(email);
expect(ky.post).toBeCalledWith('/api/session/register/passwordless/email/send-passcode', {
expect(ky.post).toBeCalledWith('/api/session/passwordless/email/send', {
json: {
email,
flow: 'register',
},
});
});
it('verifyRegisterEmailPasscode', async () => {
await verifyRegisterEmailPasscode(email, code);
expect(ky.post).toBeCalledWith('/api/session/register/passwordless/email/verify-passcode', {
expect(ky.post).toBeCalledWith('/api/session/passwordless/email/verify', {
json: {
email,
code,
flow: 'register',
},
});
expect(ky.post).toBeCalledWith('/api/session/register/passwordless/email');
});
it('sendForgotPasswordSmsPasscode', async () => {

View file

@ -1,6 +1,6 @@
import api from './api';
const registerApiPrefix = '/api/session/register';
const apiPrefix = '/api/session';
export const register = async (username: string, password: string) => {
type Response = {
@ -8,7 +8,7 @@ export const register = async (username: string, password: string) => {
};
return api
.post(`${registerApiPrefix}/username-password`, {
.post(`${apiPrefix}/register/username-password`, {
json: {
username,
password,
@ -19,9 +19,10 @@ export const register = async (username: string, password: string) => {
export const sendRegisterSmsPasscode = async (phone: string) => {
await api
.post(`${registerApiPrefix}/passwordless/sms/send-passcode`, {
.post(`${apiPrefix}/passwordless/sms/send`, {
json: {
phone,
flow: 'register',
},
})
.json();
@ -34,21 +35,23 @@ export const verifyRegisterSmsPasscode = async (phone: string, code: string) =>
redirectTo: string;
};
return api
.post(`${registerApiPrefix}/passwordless/sms/verify-passcode`, {
json: {
phone,
code,
},
})
.json<Response>();
await api.post(`${apiPrefix}/passwordless/sms/verify`, {
json: {
phone,
code,
flow: 'register',
},
});
return api.post(`${apiPrefix}/register/passwordless/sms`).json<Response>();
};
export const sendRegisterEmailPasscode = async (email: string) => {
await api
.post(`${registerApiPrefix}/passwordless/email/send-passcode`, {
.post(`${apiPrefix}/passwordless/email/send`, {
json: {
email,
flow: 'register',
},
})
.json();
@ -61,12 +64,13 @@ export const verifyRegisterEmailPasscode = async (email: string, code: string) =
redirectTo: string;
};
return api
.post(`${registerApiPrefix}/passwordless/email/verify-passcode`, {
json: {
email,
code,
},
})
.json<Response>();
await api.post(`${apiPrefix}/passwordless/email/verify`, {
json: {
email,
code,
flow: 'register',
},
});
return api.post(`${apiPrefix}/register/passwordless/email`).json<Response>();
};

View file

@ -24,9 +24,10 @@ export const signInBasic = async (username: string, password: string, socialToBi
export const sendSignInSmsPasscode = async (phone: string) => {
await api
.post('/api/session/sign-in/passwordless/sms/send-passcode', {
.post('/api/session/passwordless/sms/send', {
json: {
phone,
flow: 'sign-in',
},
})
.json();
@ -43,14 +44,15 @@ export const verifySignInSmsPasscode = async (
redirectTo: string;
};
const result = await api
.post('/api/session/sign-in/passwordless/sms/verify-passcode', {
json: {
phone,
code,
},
})
.json<Response>();
await api.post('/api/session/passwordless/sms/verify', {
json: {
phone,
code,
flow: 'sign-in',
},
});
const result = await api.post('/api/session/sign-in/passwordless/sms').json<Response>();
if (result.redirectTo && socialToBind) {
await bindSocialAccount(socialToBind);
@ -61,9 +63,10 @@ export const verifySignInSmsPasscode = async (
export const sendSignInEmailPasscode = async (email: string) => {
await api
.post('/api/session/sign-in/passwordless/email/send-passcode', {
.post('/api/session/passwordless/email/send', {
json: {
email,
flow: 'sign-in',
},
})
.json();
@ -80,14 +83,15 @@ export const verifySignInEmailPasscode = async (
redirectTo: string;
};
const result = await api
.post('/api/session/sign-in/passwordless/email/verify-passcode', {
json: {
email,
code,
},
})
.json<Response>();
await api.post('/api/session/passwordless/email/verify', {
json: {
email,
code,
flow: 'sign-in',
},
});
const result = await api.post('/api/session/sign-in/passwordless/email').json<Response>();
if (result.redirectTo && socialToBind) {
await bindSocialAccount(socialToBind);