diff --git a/Website/src/app/components/dynlist.tsx b/Website/src/app/components/dynlist.tsx index cc7bc85..c41a2ec 100644 --- a/Website/src/app/components/dynlist.tsx +++ b/Website/src/app/components/dynlist.tsx @@ -1,6 +1,6 @@ "use client"; -import { RevalidateError } from "@/utils/types"; +import { RevalidateError, UnauthorizedError } from "@/utils/types"; import { FullTrack, Vehicle } from "@/utils/api"; import useSWR from "swr"; import { coordinateFormatter } from "@/utils/helpers"; @@ -8,6 +8,7 @@ import Link from "next/link"; import TrackerCharge from "@/app/components/tracker"; import { FunctionComponent } from "react"; import { getFetcher } from "@/utils/fetcher"; +import { useRouter } from "next/navigation"; /** * A component to focus a vehicle. A link to the map view with the respective search parameter @@ -125,10 +126,13 @@ export default function DynamicList({ // sort the vehicles const sorted_vehicles = vehicles?.sort((a, b) => a.id - b.id); + // obtain the NextJS router + const router = useRouter() + if (logged_in && error) { - if (error instanceof RevalidateError && error.statusCode == 401) { + if (error instanceof UnauthorizedError || (error instanceof RevalidateError && error.statusCode === 401)) { console.log("Invalid token"); - window.location.reload(); + router.refresh(); } console.log("revalidate error", error); } diff --git a/Website/src/app/components/dynmap.tsx b/Website/src/app/components/dynmap.tsx index 4268a07..b403f64 100644 --- a/Website/src/app/components/dynmap.tsx +++ b/Website/src/app/components/dynmap.tsx @@ -1,7 +1,7 @@ "use client"; import dynamic from "next/dynamic"; import LoadMapScreen from "./loadmap"; -import { MapRefreshConfig, RevalidateError } from "@/utils/types"; +import { MapRefreshConfig, RevalidateError, UnauthorizedError } from "@/utils/types"; import useSWR from "swr"; import { useRouter } from "next/navigation"; import { useState } from "react"; @@ -51,7 +51,7 @@ export default function DynamicMap({ // log the user out if revalidation fails with a 401 response (this assumes that the request handler removed the cookie) if (logged_in && error) { - if (error instanceof RevalidateError && error.statusCode == 401) { + if (error instanceof UnauthorizedError || (error instanceof RevalidateError && error.statusCode === 401)) { console.log("Invalid token"); router.refresh(); } diff --git a/Website/src/app/components/dynmap_with_list.tsx b/Website/src/app/components/dynmap_with_list.tsx index 66eeb60..0711895 100644 --- a/Website/src/app/components/dynmap_with_list.tsx +++ b/Website/src/app/components/dynmap_with_list.tsx @@ -1,6 +1,6 @@ "use client"; -import { MapRefreshConfig, RevalidateError } from "@/utils/types"; +import { MapRefreshConfig, RevalidateError, UnauthorizedError } from "@/utils/types"; import useSWR from "swr"; import { getFetcher } from "@/utils/fetcher"; import { useRouter } from "next/navigation"; @@ -54,7 +54,7 @@ export default function DynamicMapList({ // log the user out if revalidation fails with a 401 response (this assumes that the request handler removed the cookie) if (logged_in && error) { - if (error instanceof RevalidateError && error.statusCode == 401) { + if (error instanceof UnauthorizedError || (error instanceof RevalidateError && error.statusCode === 401)) { console.log("Invalid token"); router.refresh(); }