feat: add permission-gated navigation and admin access
This commit is contained in:
@@ -42,7 +42,7 @@ export const useLoginForm = () => {
|
||||
await initializeAuthenticatedApp();
|
||||
|
||||
toast.success('Login successful');
|
||||
router.push(ROUTES.DASHBOARD);
|
||||
router.replace(ROUTES.DASHBOARD);
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : 'Unable to sign in');
|
||||
}
|
||||
|
||||
39
src/hooks/usePermissions.ts
Normal file
39
src/hooks/usePermissions.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
'use client';
|
||||
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import { useAppStore } from '@/store/app.store';
|
||||
|
||||
export type PermissionInput = string | undefined;
|
||||
|
||||
export function hasPermissionValue({
|
||||
grantedPermissions,
|
||||
permissions,
|
||||
}: {
|
||||
grantedPermissions: string[];
|
||||
permissions: PermissionInput;
|
||||
}) {
|
||||
if (!permissions) return true;
|
||||
|
||||
const normalizedPermissions = new Set(
|
||||
grantedPermissions.map((permission) => permission.toLowerCase()),
|
||||
);
|
||||
|
||||
return normalizedPermissions.has(permissions.toLowerCase());
|
||||
}
|
||||
|
||||
export function usePermissions() {
|
||||
const grantedPermissions = useAppStore((state) => state.permissions);
|
||||
|
||||
return useMemo(
|
||||
() => ({
|
||||
grantedPermissions,
|
||||
hasPermission: (permissions: PermissionInput) =>
|
||||
hasPermissionValue({
|
||||
grantedPermissions,
|
||||
permissions,
|
||||
}),
|
||||
}),
|
||||
[grantedPermissions],
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user