0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00
logto/packages/core/src/routes/well-known.ts

70 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-10-21 00:14:17 -05:00
import type { ConnectorMetadata } from '@logto/connector-kit';
import { ConnectorType } from '@logto/connector-kit';
2022-11-14 04:57:47 -05:00
import { adminConsoleApplicationId } from '@logto/schemas/lib/seeds';
import etag from 'etag';
2022-10-21 00:14:17 -05:00
import type { Provider } from 'oidc-provider';
import { getLogtoConnectors } from '@/connectors';
2022-11-14 04:57:47 -05:00
import { getApplicationIdFromInteraction } from '@/lib/session';
import { getSignInExperienceForApplication } from '@/lib/sign-in-experience';
2022-10-21 00:14:17 -05:00
import type { AnonymousRouter } from './types';
export default function wellKnownRoutes<T extends AnonymousRouter>(router: T, provider: Provider) {
router.get(
'/.well-known/sign-in-exp',
async (ctx, next) => {
2022-11-14 04:57:47 -05:00
const applicationId = await getApplicationIdFromInteraction(ctx, provider);
const [signInExperience, logtoConnectors] = await Promise.all([
2022-11-14 04:57:47 -05:00
getSignInExperienceForApplication(applicationId),
getLogtoConnectors(),
]);
const forgotPassword = {
sms: logtoConnectors.some(
({ type, dbEntry: { enabled } }) => type === ConnectorType.Sms && enabled
),
email: logtoConnectors.some(
({ type, dbEntry: { enabled } }) => type === ConnectorType.Email && enabled
),
};
2022-11-14 04:57:47 -05:00
const socialConnectors =
applicationId === adminConsoleApplicationId
? []
: signInExperience.socialSignInConnectorTargets.reduce<
Array<ConnectorMetadata & { id: string }>
>((previous, connectorTarget) => {
const connectors = logtoConnectors.filter(
({ metadata: { target }, dbEntry: { enabled } }) =>
target === connectorTarget && enabled
);
return [
...previous,
...connectors.map(({ metadata, dbEntry: { id } }) => ({ ...metadata, id })),
];
}, []);
ctx.body = {
...signInExperience,
socialConnectors,
forgotPassword,
};
return next();
},
async (ctx, next) => {
await next();
ctx.response.etag = etag(JSON.stringify(ctx.body));
if (ctx.fresh) {
ctx.status = 304;
ctx.body = null;
}
}
);
}