From 07e04eac558e0ccf4fcab6f88870d27ccdc93fc8 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Thu, 2 Mar 2023 00:44:33 +0800 Subject: [PATCH] refactor(core): fix etag --- packages/core/src/routes/well-known.ts | 90 ++++++++++++++------------ 1 file changed, 48 insertions(+), 42 deletions(-) diff --git a/packages/core/src/routes/well-known.ts b/packages/core/src/routes/well-known.ts index d00efb0f2..03c769479 100644 --- a/packages/core/src/routes/well-known.ts +++ b/packages/core/src/routes/well-known.ts @@ -17,51 +17,57 @@ export default function wellKnownRoutes( } = libraries; if (id === adminTenantId) { - router.get('/.well-known/endpoints/:tenantId', async (ctx, next) => { - if (!ctx.params.tenantId) { - throw new RequestError('request.invalid_input'); - } + router.get( + '/.well-known/endpoints/:tenantId', + async (ctx, next) => { + if (!ctx.params.tenantId) { + throw new RequestError('request.invalid_input'); + } + + ctx.body = { + user: getTenantEndpoint(ctx.params.tenantId, EnvSet.values), + }; + + return next(); + }, + koaBodyEtag() + ); + } + + router.get( + '/.well-known/sign-in-exp', + async (ctx, next) => { + const [signInExperience, logtoConnectors] = await Promise.all([ + getSignInExperience(), + getLogtoConnectors(), + ]); + + const forgotPassword = { + phone: logtoConnectors.some(({ type }) => type === ConnectorType.Sms), + email: logtoConnectors.some(({ type }) => type === ConnectorType.Email), + }; + + const socialConnectors = signInExperience.socialSignInConnectorTargets.reduce< + Array + >((previous, connectorTarget) => { + const connectors = logtoConnectors.filter( + ({ metadata: { target } }) => target === connectorTarget + ); + + return [ + ...previous, + ...connectors.map(({ metadata, dbEntry: { id } }) => ({ ...metadata, id })), + ]; + }, []); ctx.body = { - user: getTenantEndpoint(ctx.params.tenantId, EnvSet.values), + ...signInExperience, + socialConnectors, + forgotPassword, }; return next(); - }); - } - - router.get('/.well-known/sign-in-exp', async (ctx, next) => { - const [signInExperience, logtoConnectors] = await Promise.all([ - getSignInExperience(), - getLogtoConnectors(), - ]); - - const forgotPassword = { - phone: logtoConnectors.some(({ type }) => type === ConnectorType.Sms), - email: logtoConnectors.some(({ type }) => type === ConnectorType.Email), - }; - - const socialConnectors = signInExperience.socialSignInConnectorTargets.reduce< - Array - >((previous, connectorTarget) => { - const connectors = logtoConnectors.filter( - ({ metadata: { target } }) => target === connectorTarget - ); - - return [ - ...previous, - ...connectors.map(({ metadata, dbEntry: { id } }) => ({ ...metadata, id })), - ]; - }, []); - - ctx.body = { - ...signInExperience, - socialConnectors, - forgotPassword, - }; - - return next(); - }); - - router.use(koaBodyEtag()); + }, + koaBodyEtag() + ); }