Skip to content

Commit 1fdf096

Browse files
committed
Add role and permission checks for project creation button
Added `useAuth` to fetch user roles and permissions, enabling conditional rendering of the "New Project" button based on `Permissions.Projects.Create`. This improves access control and ensures unauthorized users cannot initiate new projects.
1 parent 17384d2 commit 1fdf096

File tree

2 files changed

+17
-31
lines changed

2 files changed

+17
-31
lines changed

app/apps/projects/page.tsx

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { useCallback } from 'react';
55
import {
66
Anchor,
77
Button,
8-
CardProps,
98
Container,
109
PaperProps,
1110
SimpleGrid,
@@ -18,6 +17,7 @@ import { IconPlus } from '@tabler/icons-react';
1817
import NewProjectDrawer from '@/app/apps/projects/components/NewProjectDrawer';
1918
import ProjectsCard from '@/app/apps/projects/components/ProjectsCard/ProjectsCard';
2019
import { ErrorAlert, PageHeader } from '@/components';
20+
import { useAuth } from '@/hooks/useAuth';
2121
import { PATH_DASHBOARD } from '@/routes';
2222
import { IApiResponse } from '@/types/api-response';
2323
import { IProject } from '@/types/projects';
@@ -39,13 +39,17 @@ const CARD_PROPS: Omit<PaperProps, 'children'> = {
3939
};
4040

4141
function Projects() {
42+
const { user, isAuthenticated, permissions } = useAuth();
4243
const {
4344
data: projectsData,
4445
loading: projectsLoading,
4546
error: projectsError,
4647
refetch: refetchProjects,
4748
} = useFetch<IApiResponse<IProject[]>>('/api/projects');
4849

50+
// Check if the user has a can add project
51+
const canAddProject = permissions?.includes('Permissions.Projects.Create');
52+
4953
const [newProjectOpened, { open: newProjectOpen, close: newProjectClose }] =
5054
useDisclosure(false);
5155

@@ -61,18 +65,7 @@ function Projects() {
6165
return (
6266
<Container fluid>
6367
<Stack gap="lg">
64-
<PageHeader
65-
title="Projects"
66-
breadcrumbItems={items}
67-
actionButton={
68-
<Button
69-
leftSection={<IconPlus size={18} />}
70-
onClick={newProjectOpen}
71-
>
72-
New Project
73-
</Button>
74-
}
75-
/>
68+
<PageHeader title="Projects" breadcrumbItems={items} />
7669
<SimpleGrid
7770
cols={{ base: 1, sm: 2, lg: 3, xl: 4 }}
7871
spacing={{ base: 10, sm: 'xl' }}
@@ -95,18 +88,7 @@ function Projects() {
9588
return (
9689
<Container fluid>
9790
<Stack gap="lg">
98-
<PageHeader
99-
title="Projects"
100-
breadcrumbItems={items}
101-
actionButton={
102-
<Button
103-
leftSection={<IconPlus size={18} />}
104-
onClick={newProjectOpen}
105-
>
106-
New Project
107-
</Button>
108-
}
109-
/>
91+
<PageHeader title="Projects" breadcrumbItems={items} />
11092
<ErrorAlert
11193
title="Error loading projects"
11294
message={projectsError.toString()}
@@ -131,12 +113,14 @@ function Projects() {
131113
title="Projects"
132114
breadcrumbItems={items}
133115
actionButton={
134-
<Button
135-
leftSection={<IconPlus size={18} />}
136-
onClick={newProjectOpen}
137-
>
138-
New Project
139-
</Button>
116+
canAddProject && (
117+
<Button
118+
leftSection={<IconPlus size={18} />}
119+
onClick={newProjectOpen}
120+
>
121+
New Project
122+
</Button>
123+
)
140124
}
141125
/>
142126

hooks/useAuth.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export const useAuth = () => {
3434

3535
return {
3636
user: session?.user,
37+
permissions: session?.permissions || [],
38+
roles: session?.roles || [],
3739
isAuthenticated,
3840
isLoading,
3941
login,

0 commit comments

Comments
 (0)