34 lines
887 B
TypeScript
34 lines
887 B
TypeScript
import { headers } from "next/headers"
|
|
import { redirect } from "next/navigation"
|
|
|
|
import { overview } from "@/constants/routes/myPages"
|
|
import { serverClient } from "@/lib/trpc/server"
|
|
|
|
import { auth } from "@/auth"
|
|
import { getLang } from "@/i18n/serverContext"
|
|
|
|
export default async function ProtectedLayout({
|
|
children,
|
|
}: React.PropsWithChildren) {
|
|
const session = await auth()
|
|
/**
|
|
* Fallback to make sure every route nested in the
|
|
* protected route group is actually protected.
|
|
*/
|
|
const h = headers()
|
|
const redirectTo = encodeURIComponent(
|
|
h.get("x-url") ?? h.get("x-pathname") ?? overview[getLang()]
|
|
)
|
|
|
|
if (!session) {
|
|
redirect(`/${getLang()}/login?redirectTo=${redirectTo}`)
|
|
}
|
|
|
|
const user = await serverClient().user.get()
|
|
if (!user || "error" in user) {
|
|
redirect(`/${getLang()}/login?redirectTo=${redirectTo}`)
|
|
}
|
|
|
|
return children
|
|
}
|