tests passing
This commit is contained in:
parent
322bdd3e6e
commit
b173b47925
18 changed files with 1414 additions and 93 deletions
|
|
@ -4,9 +4,21 @@ import { createContext, useContext, useState, useEffect, ReactNode } from "react
|
|||
|
||||
import { API_URL } from "./config";
|
||||
|
||||
// Permission constants matching backend
|
||||
export const Permission = {
|
||||
VIEW_COUNTER: "view_counter",
|
||||
INCREMENT_COUNTER: "increment_counter",
|
||||
USE_SUM: "use_sum",
|
||||
VIEW_AUDIT: "view_audit",
|
||||
} as const;
|
||||
|
||||
export type PermissionType = typeof Permission[keyof typeof Permission];
|
||||
|
||||
interface User {
|
||||
id: number;
|
||||
email: string;
|
||||
roles: string[];
|
||||
permissions: string[];
|
||||
}
|
||||
|
||||
interface AuthContextType {
|
||||
|
|
@ -15,6 +27,9 @@ interface AuthContextType {
|
|||
login: (email: string, password: string) => Promise<void>;
|
||||
register: (email: string, password: string) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
hasPermission: (permission: PermissionType) => boolean;
|
||||
hasAnyPermission: (...permissions: PermissionType[]) => boolean;
|
||||
hasRole: (role: string) => boolean;
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextType | null>(null);
|
||||
|
|
@ -85,8 +100,31 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||
setUser(null);
|
||||
};
|
||||
|
||||
const hasPermission = (permission: PermissionType): boolean => {
|
||||
return user?.permissions.includes(permission) ?? false;
|
||||
};
|
||||
|
||||
const hasAnyPermission = (...permissions: PermissionType[]): boolean => {
|
||||
return permissions.some((p) => user?.permissions.includes(p) ?? false);
|
||||
};
|
||||
|
||||
const hasRole = (role: string): boolean => {
|
||||
return user?.roles.includes(role) ?? false;
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ user, isLoading, login, register, logout }}>
|
||||
<AuthContext.Provider
|
||||
value={{
|
||||
user,
|
||||
isLoading,
|
||||
login,
|
||||
register,
|
||||
logout,
|
||||
hasPermission,
|
||||
hasAnyPermission,
|
||||
hasRole,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue