0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(core): fix

This commit is contained in:
Darcy Ye 2022-09-29 18:41:10 +08:00
parent aaa592fccb
commit b35fe0811d
No known key found for this signature in database
GPG key ID: B46F4C07EDEFC610
3 changed files with 32 additions and 61 deletions

View file

@ -15,7 +15,7 @@ import {
hasUserWithEmail,
hasUserWithPhone,
} from '@/queries/user';
import { verificationGuard, flowTypeGuard } from '@/routes/session/types';
import { verificationStorageParser, flowTypeGuard } from '@/routes/session/types';
import assertThat from '@/utils/assert-that';
import { AnonymousRouter } from '../types';
@ -159,18 +159,7 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
router.post(`${signInRoute}/sms`, async (ctx, next) => {
const { result } = await provider.interactionDetails(ctx.req, ctx.res);
const verificationResult = verificationGuard.safeParse(result);
assertThat(
verificationResult.success,
new RequestError({
code: 'session.verification_session_not_found',
status: 404,
})
);
const {
verification: { phone, flow, expiresAt },
} = verificationResult.data;
const { phone, flow, expiresAt } = verificationStorageParser(result);
const type = getPasswordlessRelatedLogType('sign-in', 'sms');
ctx.log(type, { phone, flow, expiresAt });
@ -194,18 +183,7 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
router.post(`${signInRoute}/email`, async (ctx, next) => {
const { result } = await provider.interactionDetails(ctx.req, ctx.res);
const verificationResult = verificationGuard.safeParse(result);
assertThat(
verificationResult.success,
new RequestError({
code: 'session.verification_session_not_found',
status: 404,
})
);
const {
verification: { email, flow, expiresAt },
} = verificationResult.data;
const { email, flow, expiresAt } = verificationStorageParser(result);
const type = getPasswordlessRelatedLogType('sign-in', 'email');
ctx.log(type, { email, flow, expiresAt });
@ -229,18 +207,7 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
router.post(`${registerRoute}/sms`, async (ctx, next) => {
const { result } = await provider.interactionDetails(ctx.req, ctx.res);
const verificationResult = verificationGuard.safeParse(result);
assertThat(
verificationResult.success,
new RequestError({
code: 'session.verification_session_not_found',
status: 404,
})
);
const {
verification: { phone, flow, expiresAt },
} = verificationResult.data;
const { phone, flow, expiresAt } = verificationStorageParser(result);
const type = getPasswordlessRelatedLogType('register', 'sms');
ctx.log(type, { phone, flow, expiresAt });
@ -264,18 +231,7 @@ export default function passwordlessRoutes<T extends AnonymousRouter>(
router.post(`${registerRoute}/email`, async (ctx, next) => {
const { result } = await provider.interactionDetails(ctx.req, ctx.res);
const verificationResult = verificationGuard.safeParse(result);
assertThat(
verificationResult.success,
new RequestError({
code: 'session.verification_session_not_found',
status: 404,
})
);
const {
verification: { email, flow, expiresAt },
} = verificationResult.data;
const { email, flow, expiresAt } = verificationStorageParser(result);
const type = getPasswordlessRelatedLogType('register', 'email');
ctx.log(type, { email, flow, expiresAt });

View file

@ -1,5 +1,8 @@
import { z } from 'zod';
import RequestError from '@/errors/RequestError';
import assertThat from '@/utils/assert-that';
export const flowTypeGuard = z.enum(['sign-in', 'register', 'forgot-password']);
export type FlowType = z.infer<typeof flowTypeGuard>;
@ -23,8 +26,19 @@ export const verificationStorageGuard = z.object({
export type VerificationStorage = z.infer<typeof verificationStorageGuard>;
export const verificationGuard = z.object({
verification: verificationStorageGuard,
});
export const verificationStorageParser = (data: unknown): VerificationStorage => {
const verificationResult = z
.object({
verification: verificationStorageGuard,
})
.safeParse(data);
assertThat(
verificationResult.success,
new RequestError({
code: 'session.verification_session_not_found',
status: 404,
})
);
export type Verification = z.infer<typeof verificationGuard>;
return verificationResult.data.verification;
};

View file

@ -19,15 +19,16 @@ export const getRoutePrefix = (
};
export const getPasscodeType = (type: FlowType) => {
if (type === 'sign-in') {
return PasscodeType.SignIn;
switch (type) {
case 'sign-in':
return PasscodeType.SignIn;
case 'register':
return PasscodeType.Register;
case 'forgot-password':
return PasscodeType.ForgotPassword;
default:
throw new RequestError({ code: 'guard.invalid_input' });
}
if (type === 'register') {
return PasscodeType.Register;
}
return PasscodeType.ForgotPassword;
};
export const getPasswordlessRelatedLogType = (