Skip to content

Commit

Permalink
Fixed Frontend invalid token handling
Browse files Browse the repository at this point in the history
  • Loading branch information
n1kPLV committed Sep 15, 2023
1 parent be9d4ee commit 59dbad9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
10 changes: 7 additions & 3 deletions Website/src/app/components/dynlist.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"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";
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
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions Website/src/app/components/dynmap.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions Website/src/app/components/dynmap_with_list.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 59dbad9

Please sign in to comment.