website refactor
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
import { PathnameInterpreter } from './PathnameInterpreter';
|
||||
import { RouteAccessPolicy } from './RouteAccessPolicy';
|
||||
import { SessionGateway } from '../gateways/SessionGateway';
|
||||
import { AuthRedirectBuilder } from './AuthRedirectBuilder';
|
||||
import type { AuthSessionDTO } from '../types/generated/AuthSessionDTO';
|
||||
import { ConsoleLogger } from '../infrastructure/logging/ConsoleLogger';
|
||||
|
||||
const logger = new ConsoleLogger();
|
||||
|
||||
export type RouteGuardResult =
|
||||
| { type: 'allow' }
|
||||
| { type: 'redirect'; to: string };
|
||||
|
||||
export class RouteGuard {
|
||||
constructor(
|
||||
private readonly interpreter: PathnameInterpreter,
|
||||
@@ -16,7 +18,7 @@ export class RouteGuard {
|
||||
private readonly builder: AuthRedirectBuilder
|
||||
) {}
|
||||
|
||||
async enforce({ pathname }: { pathname: string }): Promise<void> {
|
||||
async enforce({ pathname }: { pathname: string }): Promise<RouteGuardResult> {
|
||||
logger.info('[RouteGuard] enforce called', { pathname });
|
||||
|
||||
// Step 1: Interpret the pathname
|
||||
@@ -26,7 +28,7 @@ export class RouteGuard {
|
||||
// Step 2: Check if public non-auth page
|
||||
if (this.policy.isPublic(logicalPathname) && !this.policy.isAuthPage(logicalPathname)) {
|
||||
logger.info('[RouteGuard] Public non-auth page, allowing access');
|
||||
return; // Allow access
|
||||
return { type: 'allow' };
|
||||
}
|
||||
|
||||
// Step 3: Handle auth pages
|
||||
@@ -37,11 +39,11 @@ export class RouteGuard {
|
||||
// User is logged in, redirect away from auth page
|
||||
const redirectPath = this.builder.awayFromAuthPage({ session, currentPathname: pathname });
|
||||
logger.info('[RouteGuard] Redirecting away from auth page', { redirectPath });
|
||||
redirect(redirectPath);
|
||||
return { type: 'redirect', to: redirectPath };
|
||||
}
|
||||
// No session, allow access to auth page
|
||||
logger.info('[RouteGuard] No session, allowing access to auth page');
|
||||
return;
|
||||
return { type: 'allow' };
|
||||
}
|
||||
|
||||
// Step 4: Handle protected pages
|
||||
@@ -52,7 +54,7 @@ export class RouteGuard {
|
||||
if (!session) {
|
||||
const loginPath = this.builder.toLogin({ currentPathname: pathname });
|
||||
logger.info('[RouteGuard] No session, redirecting to login', { loginPath });
|
||||
redirect(loginPath);
|
||||
return { type: 'redirect', to: loginPath };
|
||||
}
|
||||
|
||||
// Check required roles
|
||||
@@ -61,11 +63,11 @@ export class RouteGuard {
|
||||
if (reqRoles && session.user?.role && !reqRoles.includes(session.user.role)) {
|
||||
const loginPath = this.builder.toLogin({ currentPathname: pathname });
|
||||
logger.info('[RouteGuard] Role mismatch, redirecting to login', { loginPath, reqRoles, userRole: session.user.role });
|
||||
redirect(loginPath);
|
||||
return { type: 'redirect', to: loginPath };
|
||||
}
|
||||
|
||||
// All checks passed, allow access
|
||||
logger.info('[RouteGuard] All checks passed, allowing access');
|
||||
return;
|
||||
return { type: 'allow' };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user