fix(SW-3578): Fixed session issue when sas session expires * fix(SW-3578): Fixed session issue when sas session expires * base socialLogin auto-features on validSession and being linked * remove unused object * remove 'import server-only' for isValidSession() since it's only using passed data * remove isValidClientSession() Approved-by: Joakim Jäderberg Approved-by: Anton Gunnarsson
43 lines
975 B
TypeScript
43 lines
975 B
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 { isValidSession } from "@scandic-hotels/trpc/utils/session"
|
|
|
|
import { userNotFound } from "@/constants/routes/errorPages"
|
|
|
|
import useLang from "@/hooks/useLang"
|
|
|
|
export function UserExists() {
|
|
const { data: session } = useSession()
|
|
const isUserLoggedIn = isValidSession(session)
|
|
const lang = useLang()
|
|
|
|
const { isLoading: isLoadingUser, error } = trpc.user.get.useQuery(
|
|
undefined,
|
|
{
|
|
enabled: isUserLoggedIn,
|
|
}
|
|
)
|
|
|
|
if (!isUserLoggedIn) {
|
|
return null
|
|
}
|
|
|
|
if (isLoadingUser) {
|
|
return null
|
|
}
|
|
|
|
switch (error?.data?.code) {
|
|
case "NOT_FOUND":
|
|
redirect(
|
|
`${logoutSafely[lang]}?redirectTo=${encodeURIComponent(userNotFound[lang])}`
|
|
)
|
|
default:
|
|
return null
|
|
}
|
|
}
|