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
This commit is contained in:
43
apps/scandic-web/components/UserExists.tsx
Normal file
43
apps/scandic-web/components/UserExists.tsx
Normal file
@@ -0,0 +1,43 @@
|
||||
"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
|
||||
}
|
||||
Reference in New Issue
Block a user