From 7aef3f5343aa2bafc64acbf10ee7416455c536b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o?= <43091603+glazk0@users.noreply.github.com> Date: Wed, 12 Apr 2023 13:01:54 +0200 Subject: [PATCH] Fix middleware --- middleware.ts | 56 +++++++++++++++++++++------------------- ui/UserAuthForm.tsx | 5 ++-- ui/dashboard/Usernav.tsx | 2 +- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/middleware.ts b/middleware.ts index ff53e83..aced43a 100644 --- a/middleware.ts +++ b/middleware.ts @@ -11,43 +11,22 @@ export async function middleware(req: NextRequest) { const token = req.cookies.get('token')?.value; - let isAuth = false; + const isAuth = await validateToken(token); - if (token) { - const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/player/`, { - headers: { - Authorization: `Bearer ${token}` - }, - cache: 'no-cache', - next: { - revalidate: 60 - } - }); - - if (response.status === 200) { - isAuth = true; - } - } - - if ( - (!token || !isAuth) && - (req.nextUrl.pathname.includes('dashboard') || !req.nextUrl.pathname.includes('event')) - ) { - return NextResponse.redirect(getURL('/sign-in')); - } - - if (token && !isAuth) { + if (!isAuth) { res.cookies.set('token', '', { path: '/', expires: new Date(0) }); - NextResponse.redirect(getURL('/sign-in')); + if (req.nextUrl.pathname.includes('dashboard') || req.nextUrl.pathname.includes('event')) { + return NextResponse.redirect(getURL('/sign-in')); + } return res; } - if (token && isAuth && req.nextUrl.pathname.includes('sign')) { + if (isAuth && req.nextUrl.pathname.includes('sign')) { return NextResponse.redirect(getURL('/dashboard')); } @@ -60,3 +39,26 @@ export const config = { '/((?!api|_next/static|_next/image|assets|favicon|sw.js).*)' ] }; + +async function validateToken(token: string | undefined) { + if (!token) { + return false; + } + + try { + const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/player/`, { + headers: { + Authorization: `Bearer ${token}` + }, + cache: 'no-cache', + next: { + revalidate: 60 + } + }); + + return response.ok; + } catch (error) { + console.error('Error validating token:', error); + return false; + } +} diff --git a/ui/UserAuthForm.tsx b/ui/UserAuthForm.tsx index abf9d64..0144d7c 100644 --- a/ui/UserAuthForm.tsx +++ b/ui/UserAuthForm.tsx @@ -78,12 +78,13 @@ export default function UserAuthForm() { if (res && res.ok && res.status === 200) { const token = res.headers.get('Authorization')?.split(' ')[1]; - if (token) + if (token) { cookies.set('token', token, { sameSite: 'strict', secure: process.env.NODE_ENV === 'production' }); - router.refresh(); + router.refresh(); + } } else if (res && res.status === 400) { setError('passwd', { type: 'manual', diff --git a/ui/dashboard/Usernav.tsx b/ui/dashboard/Usernav.tsx index 5252420..6136767 100644 --- a/ui/dashboard/Usernav.tsx +++ b/ui/dashboard/Usernav.tsx @@ -24,7 +24,7 @@ export default function Usernav({ isOpen, toggle }: { isOpen: boolean; toggle: ( async function handleLogout() { cookies.remove('token'); - router.replace('/'); + router.refresh(); } return (