feat(FE-113): create permissionCheck helper function

This commit is contained in:
ValdiANS
2025-10-18 13:40:32 +07:00
parent 6fe85fac13
commit 2b3aa9c3ee
+23 -2
View File
@@ -6,22 +6,43 @@ type AuthStore = {
isLoadingUser?: boolean; isLoadingUser?: boolean;
setUser: (newUserData?: UserWithRoles) => void; setUser: (newUserData?: UserWithRoles) => void;
setIsLoadingUser: (isLoading?: boolean) => void; setIsLoadingUser: (isLoading?: boolean) => void;
permissionCheck: (permissionName: string) => boolean;
}; };
const useAuthStore = create<AuthStore>()((set) => ({ const useAuthStore = create<AuthStore>()((set, get) => ({
user: undefined, user: undefined,
isLoadingUser: false, isLoadingUser: false,
setUser: (newUserData) => set({ user: newUserData }), setUser: (newUserData) => set({ user: newUserData }),
setIsLoadingUser: (isLoading) => set({ isLoadingUser: Boolean(isLoading) }), setIsLoadingUser: (isLoading) => set({ isLoadingUser: Boolean(isLoading) }),
permissionCheck: (name) => {
const { user, isLoadingUser } = get();
if (!isLoadingUser && user) {
const isAllowed = user.roles.some((role) => {
const isPermissionNameAllowed = role.permissions.some(
(permission) => permission.name === name
);
return isPermissionNameAllowed;
});
return isAllowed;
}
return false;
},
})); }));
export const useAuth = () => { export const useAuth = () => {
const { user, setUser, isLoadingUser, setIsLoadingUser } = useAuthStore(); const { user, setUser, isLoadingUser, setIsLoadingUser, permissionCheck } =
useAuthStore();
return { return {
user, user,
setUser, setUser,
isLoadingUser, isLoadingUser,
setIsLoadingUser, setIsLoadingUser,
permissionCheck,
}; };
}; };