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';
|
2022-06-27 00:22:17 -05:00
|
|
|
import {
|
|
|
|
adminConsoleApplicationId,
|
|
|
|
adminConsoleSignInMethods,
|
|
|
|
demoAppApplicationId,
|
|
|
|
} from '@logto/schemas/lib/seeds';
|
2022-06-01 03:56:23 -05:00
|
|
|
import etag from 'etag';
|
2022-06-27 00:22:17 -05:00
|
|
|
import i18next from 'i18next';
|
2022-06-20 19:56:01 -05:00
|
|
|
import { Provider, errors } 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-20 19:56:01 -05:00
|
|
|
provider.interactionDetails(ctx.req, ctx.res).catch((error: unknown) => {
|
|
|
|
// Should not block if interaction is not found
|
|
|
|
if (error instanceof errors.SessionNotFound) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}),
|
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
|
|
|
|
|
|
|
// Hard code AdminConsole sign-in methods settings.
|
2022-06-20 19:56:01 -05:00
|
|
|
if (interaction?.params.client_id === adminConsoleApplicationId) {
|
2022-06-16 21:36:07 -05:00
|
|
|
ctx.body = {
|
|
|
|
...signInExperience,
|
|
|
|
signInMethods: adminConsoleSignInMethods,
|
|
|
|
signInMode: (await hasActiveUsers()) ? SignInMode.SignIn : SignInMode.Register,
|
|
|
|
socialConnectors: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2022-06-27 00:22:17 -05:00
|
|
|
// Insert Notification Message to DemoApp
|
|
|
|
if (interaction?.params.client_id === demoAppApplicationId) {
|
|
|
|
ctx.body = {
|
|
|
|
...signInExperience,
|
|
|
|
socialConnectors,
|
|
|
|
notification: i18next.t('demo_app.notification'),
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|