46 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-05-26 18:37:36 -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-26 18:37:36 -07:00
children: React.ReactNode
2024-04-17 21:24:57 -04:00
}) {
2024-05-26 18:37:36 -07:00
const user = await currentUser()
2024-04-17 21:24:57 -04:00
if (!user) {
2024-05-26 18:37:36 -07:00
redirect("/")
2024-04-17 21:24:57 -04:00
}
const dbUser = await fetch(
2024-05-26 18:37:36 -07:00
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${user.id}`,
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
)
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(
2024-05-26 18:37:36 -07:00
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
2024-05-26 18:37:36 -07:00
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
body: JSON.stringify({
id: user.id,
name: user.firstName + " " + user.lastName,
email: user.emailAddresses[0].emailAddress,
}),
}
2024-05-26 18:37:36 -07:00
)
2024-04-17 22:55:02 -04:00
}
2024-04-17 21:24:57 -04:00
2024-05-26 18:37:36 -07:00
return <>{children}</>
2024-04-17 21:24:57 -04:00
}