40 lines
897 B
TypeScript
Raw Normal View History

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,
}: {
children: React.ReactNode;
2024-04-17 21:24:57 -04:00
}) {
const user = await currentUser();
2024-04-17 21:24:57 -04:00
if (!user) {
redirect("/");
2024-04-17 21:24:57 -04:00
}
const dbUser = await fetch(
`https://database.ishaan1013.workers.dev/api/user?id=${user.id}`
);
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) {
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-04-17 22:55:02 -04:00
}
2024-04-17 21:24:57 -04:00
return <>{children}</>;
2024-04-17 21:24:57 -04:00
}