Files
myeasycms-v2/packages/features/admin/src/components/admin-guard.tsx
giancarlo 7579ee9a2c Refactor authentication flow and improve code organization
The update implemented a redirect functionality in the multi-factor authentication flow for a better user experience. It also involved a refactoring of some parts of the code, substituting direct routing paths with path configs for easier future modifications. Import statements were adjusted for better code organization and readability.
2024-03-27 15:07:15 +08:00

24 lines
706 B
TypeScript

import { notFound } from 'next/navigation';
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
import { isSuperAdmin } from '../lib/is-super-admin';
type LayoutOrPageComponent<Params> = React.ComponentType<Params>;
export function AdminGuard<Params extends object>(
Component: LayoutOrPageComponent<Params>,
) {
return async function AdminGuardServerComponentWrapper(params: Params) {
const client = getSupabaseServerComponentClient();
const isUserSuperAdmin = await isSuperAdmin(client);
// if the user is not a super-admin, we redirect to a 404
if (!isUserSuperAdmin) {
notFound();
}
return <Component {...params} />;
};
}