Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/app/[lang]/dashboard/agents/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import ProfileLayout from "@/components/Dashboard/Profile/ProfileLayout";
import { getServerUserRole } from "@/hooks/api/getUserRole";
import { RouteParams } from "@/types";
import { UserRole } from "need4deed-sdk";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

export default async function DashboardAgentPage({ params }: RouteParams) {
const { id } = await params;

const cookieStore = await cookies();
const cookieHeader = cookieStore.toString();
const userRole = await getServerUserRole(cookieHeader);

if (!userRole || (userRole !== UserRole.COORDINATOR && userRole !== UserRole.ADMIN)) {
redirect(`/dashboard/agents`);
}
return <ProfileLayout entityId={id} entityType="agent" />;
}
27 changes: 27 additions & 0 deletions src/hooks/api/getUserRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { fetchFn } from "@/hooks/api/utils";
import { UserRole } from "need4deed-sdk";
import { apiPathMe } from "@/config/constants";

export interface ApiResponse<T> {
message: string;
data: T;
count: number;
}

export const getServerUserRole = async (cookieHeader: string): Promise<UserRole | null> => {
try {
const urlPath = apiPathMe.replace("/api/", "");
const response = await fetchFn<ApiResponse<{ role: UserRole }>>({
url: `${process.env.URL_API}/${urlPath}`,
options: {
method: "GET",
headers: { Cookie: cookieHeader },
cache: "no-store",
},
});
return response.data.role;
} catch (error) {
console.error("Failed to fetch server user role:", error);
return null;
}
};
Loading