diff --git a/app/src/app/[lng]/[inventory]/page.tsx b/app/src/app/[lng]/[inventory]/page.tsx index 3fac76b1e..c26a18b7a 100644 --- a/app/src/app/[lng]/[inventory]/page.tsx +++ b/app/src/app/[lng]/[inventory]/page.tsx @@ -9,6 +9,7 @@ import { CircleIcon } from "@/components/icons"; import { NavigationBar } from "@/components/navigation-bar"; import { useTranslation } from "@/i18n/client"; import { api, useGetCityPopulationQuery } from "@/services/api"; +import { CheckUserSession } from "@/util/check-user-session"; import { formatPercent, getShortenNumberUnit, @@ -36,6 +37,7 @@ import { Tooltip, useToast, } from "@chakra-ui/react"; +import { signIn, useSession } from "next-auth/react"; import dynamic from "next/dynamic"; import NextLink from "next/link"; import { useRouter, useParams } from "next/navigation"; @@ -76,6 +78,8 @@ export default function Home({ params: { lng } }: { params: { lng: string } }) { const { t } = useTranslation(lng, "dashboard"); const toast = useToast(); const router = useRouter(); + // Check if user is authenticated otherwise route to login page + CheckUserSession(); const { inventory: inventoryParam } = useParams(); let inventoryId = inventoryParam as string | null; if (inventoryId === "null" || inventoryId === "undefined") { @@ -384,16 +388,17 @@ export default function Home({ params: { lng } }: { params: { lng: string } }) { /> - {inventory?.city.area! == 0 || inventory?.city.area === null ? ( - - N/A - + {inventory?.city.area === null || + inventory?.city.area! === 0 ? ( + + N/A + ) : ( + > {Math.round(inventory?.city.area!)} km2 diff --git a/app/src/app/[lng]/page.tsx b/app/src/app/[lng]/page.tsx index da7796459..c7862e8b7 100644 --- a/app/src/app/[lng]/page.tsx +++ b/app/src/app/[lng]/page.tsx @@ -4,6 +4,8 @@ import { Box, Spinner, Text } from "@chakra-ui/react"; import { useRouter } from "next/navigation"; import { useEffect } from "react"; import { useTranslation } from "@/i18n/client"; +import { useSession } from "next-auth/react"; +import { CheckUserSession } from "@/util/check-user-session"; export default function HomePage({ params: { lng }, @@ -15,6 +17,8 @@ export default function HomePage({ const { data: userInfo, isLoading: isUserInfoLoading } = api.useGetUserInfoQuery(); + // Check if user is authenticated otherwise route to login page + CheckUserSession(); useEffect(() => { const defaultInventoryAvailable = !!userInfo?.defaultInventoryId; const defaultInventoryPath = `/${userInfo?.defaultInventoryId}`; diff --git a/app/src/util/check-user-session.ts b/app/src/util/check-user-session.ts new file mode 100644 index 000000000..3ea87de87 --- /dev/null +++ b/app/src/util/check-user-session.ts @@ -0,0 +1,13 @@ +import { useSession } from "next-auth/react"; +import { useRouter } from "next/navigation"; + +export const CheckUserSession = () => { + const router = useRouter(); + + const { data, status } = useSession({ + required: true, + onUnauthenticated() { + router.push("/auth/login"); + }, + }); +};