NORY-36: improve permission check in middleware
This commit is contained in:
parent
faa3485ca8
commit
40a46f6457
1 changed files with 14 additions and 8 deletions
|
@ -25,10 +25,6 @@ export async function middleware(request: NextRequest) {
|
|||
return NextResponse.redirect(url);
|
||||
}
|
||||
|
||||
if (request.nextUrl.pathname === '/unauthorised') {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
const permissionApi = await getPermissionApi();
|
||||
const isAdmin = await permissionApi.checkPermission({
|
||||
namespace: 'roles',
|
||||
|
@ -46,15 +42,25 @@ export async function middleware(request: NextRequest) {
|
|||
});
|
||||
|
||||
if (isAdmin) {
|
||||
if (request.nextUrl.pathname === '/unauthorised') {
|
||||
return redirect('/', 'HAS PERMISSION BUT ACCESSING /unauthorized');
|
||||
}
|
||||
return NextResponse.next();
|
||||
} else {
|
||||
console.log('MISSING PERMISSION');
|
||||
const url = `${process.env.NEXT_PUBLIC_DASHBOARD_NODE_URL}/unauthorised`;
|
||||
console.log('REDIRECT TO', url);
|
||||
return NextResponse.redirect(url!);
|
||||
if (request.nextUrl.pathname === '/unauthorised') {
|
||||
return NextResponse.next();
|
||||
}
|
||||
return redirect('/unauthorised', 'MISSING SESSION');
|
||||
}
|
||||
}
|
||||
|
||||
function redirect(path: string, reason: string) {
|
||||
console.log(reason);
|
||||
const url = `${process.env.NEXT_PUBLIC_DASHBOARD_NODE_URL}${path}`;
|
||||
console.log('REDIRECT TO', url);
|
||||
return NextResponse.redirect(url!);
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: '/((?!api|_next/static|_next/image|favicon.png|sitemap.xml|robots.txt).*)',
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue