feat(SW-2861): Move booking router to trpc package * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Move booking router to trpc package * Move partners router to trpc package * Move autocomplete router to trpc package * Move booking router to trpc package * Merge branch 'master' into feat/sw-2862-move-booking-router-to-trpc-package Approved-by: Linus Flood
79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { headers } from "next/headers"
|
|
import { redirect } from "next/navigation"
|
|
|
|
import { isValidSession } from "@scandic-hotels/trpc/utils/session"
|
|
|
|
import { overview } from "@/constants/routes/myPages"
|
|
import { getProfile } from "@/lib/trpc/memoizedRequests"
|
|
|
|
import { auth } from "@/auth"
|
|
import { getIntl } from "@/i18n"
|
|
import { getLang } from "@/i18n/serverContext"
|
|
|
|
export async function ProtectedLayout({ children }: React.PropsWithChildren) {
|
|
const intl = await getIntl()
|
|
const session = await auth()
|
|
/**
|
|
* Fallback to make sure every route nested in the
|
|
* protected route group is actually protected.
|
|
*/
|
|
const headersList = await headers()
|
|
const lang = await getLang()
|
|
const redirectTo = encodeURIComponent(
|
|
headersList.get("x-url") ?? headersList.get("x-pathname") ?? overview[lang]
|
|
)
|
|
|
|
const redirectURL = `/${lang}/login?redirectTo=${redirectTo}`
|
|
|
|
if (!isValidSession(session)) {
|
|
console.log(`[layout:protected] no session, redirecting to: ${redirectURL}`)
|
|
redirect(redirectURL)
|
|
}
|
|
|
|
const user = await getProfile()
|
|
|
|
if (user && "error" in user) {
|
|
// redirect(redirectURL)
|
|
console.error("[layout:protected] error in user", user)
|
|
console.error(
|
|
"[layout:protected] full user: ",
|
|
JSON.stringify(user, null, 4)
|
|
)
|
|
switch (user.cause) {
|
|
case "unauthorized": // fall through
|
|
case "forbidden": // fall through
|
|
case "token_expired":
|
|
console.error(
|
|
`[layout:protected] user error, redirecting to: ${redirectURL}`
|
|
)
|
|
redirect(redirectURL)
|
|
case "notfound":
|
|
console.error(`[layout:protected] notfound user loading error`)
|
|
break
|
|
case "unknown":
|
|
console.error(`[layout:protected] unknown user loading error`)
|
|
break
|
|
default:
|
|
console.error(`[layout:protected] unhandled user loading error`)
|
|
break
|
|
}
|
|
return (
|
|
<p>
|
|
{intl.formatMessage({
|
|
defaultMessage: "Something went wrong!",
|
|
})}
|
|
</p>
|
|
)
|
|
}
|
|
|
|
if (!user) {
|
|
console.error(
|
|
"[layout:protected] no user found, redirecting to: ",
|
|
redirectURL
|
|
)
|
|
redirect(redirectURL)
|
|
}
|
|
|
|
return children
|
|
}
|