chore(SW-3381) Moved LoginButton to design system * chore(SW-3381) Moved LoginButton to design system Approved-by: Anton Gunnarsson
44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
"use client"
|
|
|
|
import { redirect } from "next/navigation"
|
|
import { useSession } from "next-auth/react"
|
|
|
|
import { logoutSafely } from "@scandic-hotels/common/constants/routes/handleAuth"
|
|
import { trpc } from "@scandic-hotels/trpc/client"
|
|
|
|
import { userNotFound } from "@/constants/routes/errorPages"
|
|
|
|
import useLang from "@/hooks/useLang"
|
|
import { isValidClientSession } from "@/utils/clientSession"
|
|
|
|
export function UserExists() {
|
|
const { data: session } = useSession()
|
|
const isUserLoggedIn = isValidClientSession(session)
|
|
const lang = useLang()
|
|
|
|
const { data, isLoading: isLoadingUser } = trpc.user.get.useQuery(undefined, {
|
|
enabled: isUserLoggedIn,
|
|
})
|
|
|
|
if (!isUserLoggedIn) {
|
|
return null
|
|
}
|
|
|
|
if (isLoadingUser) {
|
|
return null
|
|
}
|
|
|
|
if (data && "error" in data && data.error) {
|
|
switch (data.cause) {
|
|
case "notfound":
|
|
redirect(
|
|
`${logoutSafely[lang]}?redirectTo=${encodeURIComponent(userNotFound[lang])}`
|
|
)
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
return null
|
|
}
|