diff --git a/nextjs-interface/src/app/ui/dashboard/RenameElement.tsx b/nextjs-interface/src/app/ui/dashboard/RenameElement.tsx index 64268af..e3a0546 100644 --- a/nextjs-interface/src/app/ui/dashboard/RenameElement.tsx +++ b/nextjs-interface/src/app/ui/dashboard/RenameElement.tsx @@ -31,7 +31,7 @@ export default function RenameElement({ id }: { id: string }) { } }; return ( -
+
diff --git a/nextjs-interface/src/lib/data.ts b/nextjs-interface/src/lib/data.ts index b96426c..c8a746c 100644 --- a/nextjs-interface/src/lib/data.ts +++ b/nextjs-interface/src/lib/data.ts @@ -1,12 +1,14 @@ import { useEffect, useState } from "react"; import { getToken, data, avgData, esp } from "@/lib/context"; -export const fetchWithAuth = async (url: string,token: string | null | undefined) => { +export const fetchWithAuth = async (url: string) => { try { - const response = await fetch(url, {headers: {Authorization: `Bearer ${getToken()}`}}); + const response = await fetch(url, { + headers: { Authorization: `Bearer ${getToken()}` }, + }); if (!response.ok) { if (response.status === 401) { - window.location.href = '/login'; + window.location.href = "/login"; } throw new Error(`HTTP error! Status: ${response.status}`); } @@ -27,7 +29,7 @@ export const useFetchData = ( useEffect(() => { const url = `/postgrest/rpc/avg_date?delta=${precision}&ip=eq.${ip}&and=(date.gte.${from},date.lt.${to})`; - fetchWithAuth(url,getToken()) + fetchWithAuth(url); fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } }) .then((response) => response.json()) .then((apiData: avgData[]) => { @@ -46,7 +48,7 @@ export function useLastData(type: string, ip: string) { useEffect(() => { const url = `/postgrest/data_view?limit=1&order=timestamp.desc&ip=eq.${ip}`; - fetchWithAuth(url,getToken()) + fetchWithAuth(url); fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } }) .then((response) => response.json()) .then((apiData: data[]) => { @@ -69,7 +71,7 @@ export const useAllEsp = () => { useEffect(() => { const url = `/postgrest/esp`; - fetchWithAuth(url,getToken()) + fetchWithAuth(url); fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } }) .then((response) => response.json()) .then((apiEsp: esp[]) => { @@ -88,7 +90,7 @@ export default function useFindIpById(id: string) { useEffect(() => { const url = `/postgrest/esp?select=ip&id=eq.${id}`; - fetchWithAuth(url,getToken()) + fetchWithAuth(url); fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } }) .then((response) => response.json()) .then((apiIp: esp[]) => { @@ -107,7 +109,7 @@ export function useFindNameById(id: string) { useEffect(() => { const url = `/postgrest/esp?select=name&id=eq.${id}`; - fetchWithAuth(url,getToken()) + fetchWithAuth(url); fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } }) .then((response) => response.json()) .then((apiIp: esp[]) => { @@ -126,7 +128,7 @@ export function GetEspPosition(id: string) { useEffect(() => { const url = `/postgrest/esp?select=x,y,name,ip&id=eq.${id}`; - fetchWithAuth(url,getToken()) + fetchWithAuth(url); fetch(url, { headers: { Authorization: `Bearer ${getToken()}` } }) .then((response) => response.json()) .then((apiPosition: esp[]) => { diff --git a/nextjs-interface/src/middleware.tsx b/nextjs-interface/src/middleware.tsx index dfad80d..1a2909e 100644 --- a/nextjs-interface/src/middleware.tsx +++ b/nextjs-interface/src/middleware.tsx @@ -1,17 +1,13 @@ import { NextResponse } from "next/server"; import type { NextRequest } from "next/server"; -import {fetchWithAuth} from "@/lib/data"; export async function middleware(request: NextRequest) { - - const token = request.cookies.get("token"); - const {pathname} = request.nextUrl; + const { pathname } = request.nextUrl; if (pathname === "/" && !token) { const loginUrl = new URL("/login", request.nextUrl.origin).toString(); return NextResponse.redirect(loginUrl); - } if ((pathname === "/dashboard" || pathname === "/dashboard/plan") && !token) { @@ -28,5 +24,11 @@ export async function middleware(request: NextRequest) { } export const config = { - matcher: ["/", "/dashboard", "/login", "/dashboard/plan", "/dashboard/esp/:path*"], + matcher: [ + "/", + "/dashboard", + "/login", + "/dashboard/plan", + "/dashboard/esp/:path*", + ], };