mirror of
https://github.com/logto-io/logto.git
synced 2025-03-10 22:22:45 -05:00
refactor(core): per review
add the missing file refactor: remove unused code fix(core): rename interactionSie middleware rename interactionSie middleware fix(core): update context type name update context type name refactor: per review
This commit is contained in:
parent
f8000fe18d
commit
baaa58ca0b
7 changed files with 65 additions and 36 deletions
|
@ -87,6 +87,9 @@ function createEnvSet() {
|
|||
|
||||
return queryClient;
|
||||
},
|
||||
get queryClientSafe() {
|
||||
return queryClient;
|
||||
},
|
||||
get oidc() {
|
||||
if (!oidc) {
|
||||
return throwNotLoadedError();
|
||||
|
|
|
@ -1,16 +1,24 @@
|
|||
import { noop } from '@silverhand/essentials';
|
||||
import Koa from 'koa';
|
||||
|
||||
import { configDotEnv } from './env-set/dot-env.js';
|
||||
import envSet from './env-set/index.js';
|
||||
import initI18n from './i18n/init.js';
|
||||
|
||||
await configDotEnv();
|
||||
await envSet.load();
|
||||
const app = new Koa({
|
||||
proxy: envSet.values.trustProxyHeader,
|
||||
});
|
||||
await initI18n();
|
||||
try {
|
||||
await configDotEnv();
|
||||
await envSet.load();
|
||||
const app = new Koa({
|
||||
proxy: envSet.values.trustProxyHeader,
|
||||
});
|
||||
await initI18n();
|
||||
|
||||
// Import last until init completed
|
||||
const { default: initApp } = await import('./app/init.js');
|
||||
await initApp(app);
|
||||
// Import last until init completed
|
||||
const { default: initApp } = await import('./app/init.js');
|
||||
await initApp(app);
|
||||
} catch (error: unknown) {
|
||||
console.error('Error while initializing app:');
|
||||
console.error(error);
|
||||
|
||||
await Promise.all([envSet.poolSafe?.end(), envSet.queryClientSafe?.end()]).catch(noop);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Event, userInfoSelectFields } from '@logto/schemas';
|
||||
import { HookEventPayload, HookEvent } from '@logto/schemas/models';
|
||||
import { trySafe } from '@logto/shared';
|
||||
import { conditional } from '@silverhand/essentials';
|
||||
import { conditional, pick } from '@silverhand/essentials';
|
||||
import { got } from 'got';
|
||||
import type { Provider } from 'oidc-provider';
|
||||
|
||||
|
@ -16,17 +16,6 @@ const eventToHook: Record<Event, HookEvent> = {
|
|||
[Event.ForgotPassword]: HookEvent.PostResetPassword,
|
||||
};
|
||||
|
||||
// TODO: replace `lodash.pick`
|
||||
const pick = <T, Keys extends Array<keyof T>>(
|
||||
object: T,
|
||||
...keys: Keys
|
||||
): { [key in Keys[number]]: T[key] } => {
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
return Object.fromEntries(keys.map((key) => [key, object[key]])) as {
|
||||
[key in Keys[number]]: T[key];
|
||||
};
|
||||
};
|
||||
|
||||
export type Interaction = Awaited<ReturnType<Provider['interactionDetails']>>;
|
||||
|
||||
export const triggerInteractionHooksIfNeeded = async (
|
||||
|
|
|
@ -15,7 +15,7 @@ import submitInteraction from './actions/submit-interaction.js';
|
|||
import koaInteractionDetails from './middleware/koa-interaction-details.js';
|
||||
import type { WithInteractionDetailsContext } from './middleware/koa-interaction-details.js';
|
||||
import koaInteractionHooks from './middleware/koa-interaction-hooks.js';
|
||||
import koaInteractionSIE from './middleware/koa-interaction-sie.js';
|
||||
import koaInteractionSie from './middleware/koa-interaction-sie.js';
|
||||
import { sendPasscodePayloadGuard, socialAuthorizationUrlPayloadGuard } from './types/guard.js';
|
||||
import {
|
||||
getInteractionStorage,
|
||||
|
@ -63,7 +63,7 @@ export default function interactionRoutes<T extends AnonymousRouter>(
|
|||
profile: profileGuard.optional(),
|
||||
}),
|
||||
}),
|
||||
koaInteractionSIE(),
|
||||
koaInteractionSie(),
|
||||
async (ctx, next) => {
|
||||
const { event, identifier, profile } = ctx.guard.body;
|
||||
const { signInExperience } = ctx;
|
||||
|
@ -108,7 +108,7 @@ export default function interactionRoutes<T extends AnonymousRouter>(
|
|||
router.put(
|
||||
`${interactionPrefix}/event`,
|
||||
koaGuard({ body: z.object({ event: eventGuard }) }),
|
||||
koaInteractionSIE(),
|
||||
koaInteractionSie(),
|
||||
async (ctx, next) => {
|
||||
const { event } = ctx.guard.body;
|
||||
const { signInExperience, interactionDetails } = ctx;
|
||||
|
@ -141,7 +141,7 @@ export default function interactionRoutes<T extends AnonymousRouter>(
|
|||
koaGuard({
|
||||
body: identifierPayloadGuard,
|
||||
}),
|
||||
koaInteractionSIE(),
|
||||
koaInteractionSie(),
|
||||
async (ctx, next) => {
|
||||
const identifierPayload = ctx.guard.body;
|
||||
const { signInExperience, interactionDetails } = ctx;
|
||||
|
@ -172,7 +172,7 @@ export default function interactionRoutes<T extends AnonymousRouter>(
|
|||
koaGuard({
|
||||
body: profileGuard,
|
||||
}),
|
||||
koaInteractionSIE(),
|
||||
koaInteractionSie(),
|
||||
async (ctx, next) => {
|
||||
const profilePayload = ctx.guard.body;
|
||||
const { signInExperience, interactionDetails } = ctx;
|
||||
|
@ -213,7 +213,7 @@ export default function interactionRoutes<T extends AnonymousRouter>(
|
|||
// Submit Interaction
|
||||
router.post(
|
||||
`${interactionPrefix}/submit`,
|
||||
koaInteractionSIE(),
|
||||
koaInteractionSie(),
|
||||
koaInteractionHooks(),
|
||||
async (ctx, next) => {
|
||||
const { interactionDetails } = ctx;
|
||||
|
@ -260,11 +260,11 @@ export default function interactionRoutes<T extends AnonymousRouter>(
|
|||
body: sendPasscodePayloadGuard,
|
||||
}),
|
||||
async (ctx, next) => {
|
||||
const { interactionDetails, guard, createLog } = ctx;
|
||||
// Check interaction exists
|
||||
getInteractionStorage(ctx.interactionDetails.result);
|
||||
getInteractionStorage(interactionDetails.result);
|
||||
|
||||
const { jti } = await provider.interactionDetails(ctx.req, ctx.res);
|
||||
await sendPasscodeToIdentifier(ctx.guard.body, jti, ctx.createLog);
|
||||
await sendPasscodeToIdentifier(guard.body, interactionDetails.jti, createLog);
|
||||
|
||||
ctx.status = 204;
|
||||
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
import type { SignInExperience } from '@logto/schemas';
|
||||
import { conditional } from '@silverhand/essentials';
|
||||
import type { MiddlewareType } from 'koa';
|
||||
|
||||
import { getSignInExperienceForApplication } from '#src/libraries/sign-in-experience/index.js';
|
||||
|
||||
import type { WithInteractionDetailsContext } from './koa-interaction-details.js';
|
||||
|
||||
export type WithInteractionSieContext<ContextT> = WithInteractionDetailsContext<ContextT> & {
|
||||
signInExperience: SignInExperience;
|
||||
};
|
||||
|
||||
export default function koaInteractionSie<StateT, ContextT, ResponseT>(): MiddlewareType<
|
||||
StateT,
|
||||
WithInteractionSieContext<ContextT>,
|
||||
ResponseT
|
||||
> {
|
||||
return async (ctx, next) => {
|
||||
const { interactionDetails } = ctx;
|
||||
|
||||
const signInExperience = await getSignInExperienceForApplication(
|
||||
conditional(
|
||||
typeof interactionDetails.params.client_id === 'string' &&
|
||||
interactionDetails.params.client_id
|
||||
)
|
||||
);
|
||||
|
||||
ctx.signInExperience = signInExperience;
|
||||
|
||||
return next();
|
||||
};
|
||||
}
|
|
@ -7,7 +7,7 @@ import RequestError from '#src/errors/RequestError/index.js';
|
|||
import { findUserById } from '#src/queries/user.js';
|
||||
import assertThat from '#src/utils/assert-that.js';
|
||||
|
||||
import type { WithInteractionSIEContext } from '../middleware/koa-interaction-sie.js';
|
||||
import type { WithInteractionSieContext } from '../middleware/koa-interaction-sie.js';
|
||||
import type { IdentifierVerifiedInteractionResult } from '../types/index.js';
|
||||
import { isUserPasswordSet } from '../utils/index.js';
|
||||
|
||||
|
@ -70,7 +70,7 @@ const getMissingProfileBySignUpIdentifiers = ({
|
|||
};
|
||||
|
||||
export default async function validateMandatoryUserProfile(
|
||||
ctx: WithInteractionSIEContext<Context>,
|
||||
ctx: WithInteractionSieContext<Context>,
|
||||
interaction: IdentifierVerifiedInteractionResult
|
||||
) {
|
||||
const { signUp } = ctx.signInExperience;
|
||||
|
|
|
@ -2,7 +2,7 @@ import { generateStandardId } from '@logto/core-kit';
|
|||
import { createModel } from '@withtyped/server';
|
||||
import { z } from 'zod';
|
||||
|
||||
import type { Application, Connector, User } from '../db-entries/index.js';
|
||||
import type { Application, User } from '../db-entries/index.js';
|
||||
import type { userInfoSelectFields } from '../types/index.js';
|
||||
|
||||
export enum HookEvent {
|
||||
|
@ -20,9 +20,6 @@ export type HookEventPayload = {
|
|||
userId?: string;
|
||||
user?: Pick<User, typeof userInfoSelectFields[number]>;
|
||||
application?: Pick<Application, 'id' | 'type' | 'name' | 'description'>;
|
||||
connectors?: Array<
|
||||
Pick<Connector, 'id'> & Pick<Connector['metadata'], 'name'> & Record<string, unknown>
|
||||
>;
|
||||
} & Record<string, unknown>;
|
||||
|
||||
export type HookConfig = {
|
||||
|
|
Loading…
Add table
Reference in a new issue