Files
Inventory_frontend/app/middleware/profile.global.ts
Matthieu cc70fe2b29 feat(permissions) : add role-based UI guards and readonly mode for viewers
- Add usePermissions composable (isAdmin, canEdit, canView)
- Password-protected profile login with modal on profiles page
- Disable all form fields for ROLE_VIEWER across edit/create pages
- Show navigation buttons (Modifier/Consulter) for all roles, hide delete for viewers
- Add readonly prop to ModelTypeForm for category pages
- Disable modal fields (sites, constructeurs) for viewers
- Guard /admin routes in middleware

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 13:36:42 +01:00

34 lines
972 B
TypeScript

import { useProfileSession, usePermissions } from "#imports";
export default defineNuxtRouteMiddleware(async (to) => {
const { ensureSession, activeProfile } = useProfileSession();
await ensureSession();
const rawPath = to?.path ?? "";
const normalizedPath = rawPath.startsWith("/") ? rawPath : `/${rawPath}`;
const fullPath = to?.fullPath ?? normalizedPath;
const routeName = typeof to?.name === "string" ? to.name : "";
const isProfilesRoute =
normalizedPath.startsWith("/profiles") ||
fullPath.startsWith("/profiles") ||
routeName.startsWith("profiles");
// Redirect to login if no active profile
if (!activeProfile.value && !isProfilesRoute) {
return navigateTo("/profiles");
}
// Permission checks
if (activeProfile.value) {
const { isAdmin } = usePermissions();
// Admin-only routes
if (normalizedPath.startsWith("/admin")) {
if (!isAdmin.value) {
return navigateTo("/");
}
}
}
});