Files
web/apps/scandic-web/components/UserExists.tsx
Hrishikesh Vaipurkar 260a544c99 Merged in chore/SW-3381-move-loginbutton-to-ds- (pull request #2752)
chore(SW-3381) Moved LoginButton to design system

* chore(SW-3381) Moved LoginButton to design system


Approved-by: Anton Gunnarsson
2025-09-03 09:11:28 +00:00

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
}