39 lines
871 B
TypeScript
Raw Normal View History

2024-05-03 13:53:21 -07:00
import { User } from "@/lib/types"
2024-04-17 21:24:57 -04:00
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("/")
}
2024-04-17 22:55:02 -04:00
const dbUser = await fetch(`http://localhost:8787/api/user?id=${user.id}`)
2024-05-03 13:53:21 -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-04-17 22:55:02 -04:00
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)
2024-04-17 22:55:02 -04:00
} else {
// user already exists in db
}
2024-04-17 21:24:57 -04:00
return <>{children}</>
}