2024-05-07 22:40:59 -07:00
|
|
|
import { User } from "@/lib/types";
|
|
|
|
import { currentUser } from "@clerk/nextjs";
|
|
|
|
import { redirect } from "next/navigation";
|
2024-04-17 21:24:57 -04:00
|
|
|
|
|
|
|
export default async function AppAuthLayout({
|
|
|
|
children,
|
|
|
|
}: {
|
2024-05-07 22:40:59 -07:00
|
|
|
children: React.ReactNode;
|
2024-04-17 21:24:57 -04:00
|
|
|
}) {
|
2024-05-07 22:40:59 -07:00
|
|
|
const user = await currentUser();
|
2024-04-17 21:24:57 -04:00
|
|
|
|
|
|
|
if (!user) {
|
2024-05-07 22:40:59 -07:00
|
|
|
redirect("/");
|
2024-04-17 21:24:57 -04:00
|
|
|
}
|
|
|
|
|
2024-05-05 22:33:24 -07:00
|
|
|
const dbUser = await fetch(
|
|
|
|
`https://database.ishaan1013.workers.dev/api/user?id=${user.id}`
|
2024-05-07 22:40:59 -07:00
|
|
|
);
|
|
|
|
const dbUserJSON = (await dbUser.json()) as User;
|
2024-04-17 21:24:57 -04:00
|
|
|
|
2024-05-03 13:53:21 -07:00
|
|
|
if (!dbUserJSON.id) {
|
2024-05-05 22:33:24 -07:00
|
|
|
const res = await fetch(
|
|
|
|
"https://database.ishaan1013.workers.dev/api/user",
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
id: user.id,
|
|
|
|
name: user.firstName + " " + user.lastName,
|
|
|
|
email: user.emailAddresses[0].emailAddress,
|
|
|
|
}),
|
|
|
|
}
|
2024-05-07 22:40:59 -07:00
|
|
|
);
|
2024-04-17 22:55:02 -04:00
|
|
|
}
|
2024-04-17 21:24:57 -04:00
|
|
|
|
2024-05-07 22:40:59 -07:00
|
|
|
return <>{children}</>;
|
2024-04-17 21:24:57 -04:00
|
|
|
}
|