Fix middleware
This commit is contained in:
parent
a87386fa5b
commit
7aef3f5343
3 changed files with 33 additions and 30 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -24,7 +24,7 @@ export default function Usernav({ isOpen, toggle }: { isOpen: boolean; toggle: (
|
|||
|
||||
async function handleLogout() {
|
||||
cookies.remove('token');
|
||||
router.replace('/');
|
||||
router.refresh();
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
Loading…
Add table
Reference in a new issue