ignore folder
This commit is contained in:
11
hooks/useApi.ts
Normal file
11
hooks/useApi.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { AxiosError } from "axios";
|
||||
|
||||
export async function useApi<T>(promise: Promise<{ data: T }>): Promise<T> {
|
||||
try {
|
||||
const response = await promise;
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
const message = (error as AxiosError<{ message?: string }>).response?.data?.message ?? "Unexpected error";
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
5
hooks/useAuth.ts
Normal file
5
hooks/useAuth.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
export function useAuth() {
|
||||
return useAuthStore((state) => state);
|
||||
}
|
||||
23
hooks/usePermissions.ts
Normal file
23
hooks/usePermissions.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { usePermissionStore } from "@/store/permissionStore";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
export function usePermissions() {
|
||||
const roles = usePermissionStore((s) => s.roles);
|
||||
const permissions = usePermissionStore((s) => s.permissions);
|
||||
const isAuthenticated = useAuthStore((s) => !!s.accessToken);
|
||||
|
||||
const hasPermission = (permission: string) => permissions.includes(permission);
|
||||
const hasRole = (role: string) => roles.includes(role);
|
||||
|
||||
return {
|
||||
roles,
|
||||
permissions,
|
||||
isAuthenticated,
|
||||
isAdmin: hasRole("ADMIN"),
|
||||
isAdminOrManager: hasRole("ADMIN") || hasRole("USER_ROLE_ADMIN"),
|
||||
canManageUsers: hasPermission("USER_MANAGE") || hasRole("USER_ROLE_ADMIN"),
|
||||
canManageRoles: hasPermission("ROLE_MANAGE") || hasRole("USER_ROLE_ADMIN"),
|
||||
canApproveWorkflow: hasPermission("WORKFLOW_APPROVE") || hasRole("CHECKER"),
|
||||
canReadProfile: hasPermission("USER_READ") || hasRole("ADMIN")
|
||||
};
|
||||
}
|
||||
6
hooks/useTenantHeader.ts
Normal file
6
hooks/useTenantHeader.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { useTenantStore } from "@/store/tenantStore";
|
||||
|
||||
export function useTenantHeader() {
|
||||
const tenantId = useTenantStore((s) => s.tenantId);
|
||||
return tenantId;
|
||||
}
|
||||
Reference in New Issue
Block a user