Skip to content

Commit

Permalink
fix: moved usesession hook to a reusable function, reworked the area …
Browse files Browse the repository at this point in the history
…login
  • Loading branch information
cephaschapa committed Apr 29, 2024
1 parent 19fc6a2 commit 932f769
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
12 changes: 4 additions & 8 deletions app/src/app/[lng]/[inventory]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -78,12 +79,7 @@ export default function Home({ params: { lng } }: { params: { lng: string } }) {
const toast = useToast();
const router = useRouter();
// Check if user is authenticated otherwise route to login page
const { data, status } = useSession({
required: true,
onUnauthenticated() {
router.push("/auth/login");
},
});
checkUserSession();
const { inventory: inventoryParam } = useParams();
let inventoryId = inventoryParam as string | null;
if (inventoryId === "null" || inventoryId === "undefined") {
Expand Down Expand Up @@ -392,8 +388,8 @@ export default function Home({ params: { lng } }: { params: { lng: string } }) {
/>
<Box>
<Box className="flex gap-1">
{inventory?.city.area! == 0 ||
inventory?.city.area === null ? (
{inventory?.city.area === null ||
inventory?.city.area! === 0 ? (
<Text
fontFamily="heading"
color="border.neutral"
Expand Down
9 changes: 2 additions & 7 deletions app/src/app/[lng]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 },
Expand All @@ -17,13 +18,7 @@ export default function HomePage({
api.useGetUserInfoQuery();

// Check if user is authenticated otherwise route to login page
const { data, status } = useSession({
required: true,
onUnauthenticated() {
router.push("/auth/login");
},
});

checkUserSession();
useEffect(() => {
const defaultInventoryAvailable = !!userInfo?.defaultInventoryId;
const defaultInventoryPath = `/${userInfo?.defaultInventoryId}`;
Expand Down
13 changes: 13 additions & 0 deletions app/src/util/check-user-session.ts
Original file line number Diff line number Diff line change
@@ -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");
},
});
};

0 comments on commit 932f769

Please sign in to comment.