Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
alan2207 committed Nov 12, 2024
1 parent 44a1c50 commit ea65ef2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
19 changes: 19 additions & 0 deletions apps/nextjs-app/src/app/app/users/_components/admin-guard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use client';

import { Spinner } from '@/components/ui/spinner';
import { useUser } from '@/lib/auth';
import { canViewUsers } from '@/lib/authorization';

export const AdminGuard = ({ children }: { children: React.ReactNode }) => {
const user = useUser();

if (!user?.data) {
return <Spinner className="m-4" />;
}

if (!canViewUsers(user?.data)) {
return <div>Only admin can view this.</div>;
}

return children;
};
8 changes: 0 additions & 8 deletions apps/nextjs-app/src/app/app/users/_components/users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@ import {

import { getUsersQueryOptions } from '@/features/users/api/get-users';
import { UsersList } from '@/features/users/components/users-list';
import { getUser } from '@/lib/auth';
import { canViewUsers } from '@/lib/authorization';

export const Users = async () => {
const user = await getUser();

if (!canViewUsers(user)) {
return <div>Only admin can view this.</div>;
}

const queryClient = new QueryClient();

await queryClient.prefetchQuery(getUsersQueryOptions());
Expand Down
5 changes: 4 additions & 1 deletion apps/nextjs-app/src/app/app/users/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ContentLayout } from '@/components/layouts/content-layout';

import { AdminGuard } from './_components/admin-guard';
import { Users } from './_components/users';

export const metadata = {
Expand All @@ -10,7 +11,9 @@ export const metadata = {
const UsersPage = () => {
return (
<ContentLayout title="Users">
<Users />
<AdminGuard>
<Users />
</AdminGuard>
</ContentLayout>
);
};
Expand Down
2 changes: 2 additions & 0 deletions apps/nextjs-app/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ const RootLayout = async ({ children }: { children: ReactNode }) => {

export default RootLayout;

// We are not prerendering anything because the app is highly dynamic
// and the data depends on the user so we need to send cookies with each request
export const dynamic = 'force-dynamic';

0 comments on commit ea65ef2

Please sign in to comment.