File size: 856 Bytes
13ae717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { apiServer } from "@/lib/api";
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
import { MyProjects } from "@/components/my-projects";

async function getMyProjects() {
  const cookieStore = await cookies();
  const token = cookieStore.get(MY_TOKEN_KEY())?.value;
  if (!token) return { redirectUrl: true, projects: [] };
  try {
    const { data } = await apiServer.get("/me/projects", {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });

    return {
      projects: data.projects,
    };
  } catch {
    return { projects: [] };
  }
}
export default async function ProjectsPage() {
  const { redirectUrl, projects } = await getMyProjects();
  if (redirectUrl) {
    redirect("/");
  }

  return <MyProjects projects={projects} />;
}