Skip to content

Commit 4a39392

Browse files
Copilothotlong
andcommitted
Fix useHasPermission to handle both array and object permission types
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent c9cc790 commit 4a39392

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

src/client/hooks.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,29 @@ export function usePermissions() {
7777

7878
/**
7979
* Convenience hook to check if user has a specific permission
80+
* Supports both array and object-based permission formats
8081
*/
8182
export function useHasPermission(permission: string) {
8283
const { permissions, isPending } = usePermissions();
8384

85+
const hasPermission = (() => {
86+
if (!permissions) return false;
87+
88+
// Array format: ['user.read', 'user.write']
89+
if (Array.isArray(permissions)) {
90+
return permissions.includes(permission);
91+
}
92+
93+
// Object format: { 'user.read': true, 'user.write': false }
94+
if (typeof permissions === 'object') {
95+
return permissions[permission] === true;
96+
}
97+
98+
return false;
99+
})();
100+
84101
return {
85-
hasPermission: permissions?.includes?.(permission) ?? false,
102+
hasPermission,
86103
isPending,
87104
};
88105
}

0 commit comments

Comments
 (0)