Files
n8n-enterprise-unlocked/packages/frontend/editor-ui/src/utils/rbac/middleware/authenticated.ts
Andreas Fitzek 657e5a3b3a feat(core): Allow enforcement of MFA usage on instance (#16556)
Co-authored-by: Marc Littlemore <marc@n8n.io>
Co-authored-by: Csaba Tuncsik <csaba.tuncsik@gmail.com>
2025-07-02 11:03:10 +02:00

32 lines
1.0 KiB
TypeScript

import type { RouterMiddleware } from '@/types/router';
import { VIEWS } from '@/constants';
import type { AuthenticatedPermissionOptions } from '@/types/rbac';
import { isAuthenticated, shouldEnableMfa } from '@/utils/rbac/checks';
export const authenticatedMiddleware: RouterMiddleware<AuthenticatedPermissionOptions> = async (
to,
_from,
next,
options,
) => {
// ensure that we are removing the already existing redirect query parameter
// to avoid infinite redirect loops
const url = new URL(window.location.href);
url.searchParams.delete('redirect');
const redirect = to.query.redirect ?? encodeURIComponent(`${url.pathname}${url.search}`);
const valid = isAuthenticated(options);
if (!valid) {
return next({ name: VIEWS.SIGNIN, query: { redirect } });
}
// If MFA is not enabled, and the instance enforces MFA, redirect to personal settings
const mfaNeeded = shouldEnableMfa();
if (mfaNeeded) {
if (to.name !== VIEWS.PERSONAL_SETTINGS) {
return next({ name: VIEWS.PERSONAL_SETTINGS, query: { redirect } });
}
return;
}
};