2022-05-18 22:24:42 -05:00
|
|
|
import { ConnectorMetadata } from '@logto/connector-types';
|
2022-06-16 21:36:07 -05:00
|
|
|
import { SignInMode } from '@logto/schemas';
|
|
|
|
import { adminConsoleApplicationId, adminConsoleSignInMethods } from '@logto/schemas/lib/seeds';
|
2022-06-01 03:56:23 -05:00
|
|
|
import etag from 'etag';
|
2022-06-16 21:36:07 -05:00
|
|
|
import { Provider } from 'oidc-provider';
|
2022-05-18 22:24:42 -05:00
|
|
|
|
|
|
|
import { getConnectorInstances } from '@/connectors';
|
|
|
|
import { findDefaultSignInExperience } from '@/queries/sign-in-experience';
|
2022-06-16 21:36:07 -05:00
|
|
|
import { hasActiveUsers } from '@/queries/user';
|
2022-05-18 22:24:42 -05:00
|
|
|
|
|
|
|
import { AnonymousRouter } from './types';
|
|
|
|
|
2022-06-16 21:36:07 -05:00
|
|
|
export default function signInSettingsRoutes<T extends AnonymousRouter>(
|
|
|
|
router: T,
|
|
|
|
provider: Provider
|
|
|
|
) {
|
2022-06-01 03:56:23 -05:00
|
|
|
router.get(
|
|
|
|
'/sign-in-settings',
|
|
|
|
async (ctx, next) => {
|
2022-06-16 21:36:07 -05:00
|
|
|
const [signInExperience, connectorInstances, interaction] = await Promise.all([
|
2022-06-01 03:56:23 -05:00
|
|
|
findDefaultSignInExperience(),
|
|
|
|
getConnectorInstances(),
|
2022-06-16 21:36:07 -05:00
|
|
|
provider.interactionDetails(ctx.req, ctx.res),
|
2022-06-01 03:56:23 -05:00
|
|
|
]);
|
2022-06-16 21:36:07 -05:00
|
|
|
|
2022-06-01 03:56:23 -05:00
|
|
|
const socialConnectors = signInExperience.socialSignInConnectorTargets.reduce<
|
|
|
|
Array<ConnectorMetadata & { id: string }>
|
|
|
|
>((previous, connectorTarget) => {
|
|
|
|
const connectors = connectorInstances.filter(
|
|
|
|
({ metadata: { target }, connector: { enabled } }) =>
|
|
|
|
target === connectorTarget && enabled
|
|
|
|
);
|
2022-05-18 22:24:42 -05:00
|
|
|
|
2022-06-01 03:56:23 -05:00
|
|
|
return [
|
|
|
|
...previous,
|
|
|
|
...connectors.map(({ metadata, connector: { id } }) => ({ ...metadata, id })),
|
|
|
|
];
|
|
|
|
}, []);
|
2022-06-16 21:36:07 -05:00
|
|
|
|
|
|
|
const {
|
|
|
|
params: { client_id },
|
|
|
|
} = interaction;
|
|
|
|
|
|
|
|
// Hard code AdminConsole sign-in methods settings.
|
|
|
|
if (client_id === adminConsoleApplicationId) {
|
|
|
|
ctx.body = {
|
|
|
|
...signInExperience,
|
|
|
|
signInMethods: adminConsoleSignInMethods,
|
|
|
|
signInMode: (await hasActiveUsers()) ? SignInMode.SignIn : SignInMode.Register,
|
|
|
|
socialConnectors: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2022-06-01 03:56:23 -05:00
|
|
|
ctx.body = { ...signInExperience, socialConnectors };
|
2022-05-18 22:24:42 -05:00
|
|
|
|
2022-06-01 03:56:23 -05:00
|
|
|
return next();
|
|
|
|
},
|
|
|
|
async (ctx, next) => {
|
|
|
|
await next();
|
|
|
|
|
|
|
|
ctx.response.etag = etag(JSON.stringify(ctx.body));
|
|
|
|
|
|
|
|
if (ctx.fresh) {
|
|
|
|
ctx.status = 304;
|
|
|
|
ctx.body = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-05-18 22:24:42 -05:00
|
|
|
}
|