2024-05-01 01:29:16 -04:00

39 lines
879 B
TypeScript

import { currentUser } from "@clerk/nextjs"
import { redirect } from "next/navigation"
export default async function AppAuthLayout({
children,
}: {
children: React.ReactNode
}) {
const user = await currentUser()
if (!user) {
redirect("/")
}
const dbUser = await fetch(`http://localhost:8787/api/user?id=${user.id}`)
const dbUserJSON = await dbUser.json()
if (!dbUserJSON?.id) {
const res = await fetch("http://localhost:8787/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,
}),
})
console.log(res)
} else {
// user already exists in db
console.log("user exists already.", dbUserJSON)
}
return <>{children}</>
}