Files
web/apps/scandic-web/components/UserExists.tsx
Linus Flood e2544f9f89 Merged in feat/sw-3192-no-user (pull request #2680)
feat(SW-3192): Checks if user exists, otherwise logout and show error

* feat(SW-3192): Checks if user exists, otherwise logout and show error
2025-08-22 09:47:54 +00:00

44 lines
1004 B
TypeScript

"use client"
import { redirect } from "next/navigation"
import { useSession } from "next-auth/react"
import { trpc } from "@scandic-hotels/trpc/client"
import { userNotFound } from "@/constants/routes/errorPages"
import { logoutSafely } from "@/constants/routes/handleAuth"
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
}