72 lines
1.4 KiB
TypeScript
72 lines
1.4 KiB
TypeScript
/**
|
|
* Component: AuthGuard
|
|
*
|
|
* Protects routes that require authentication but not specific roles.
|
|
* Uses the same Gateway pattern for consistency.
|
|
*/
|
|
|
|
'use client';
|
|
|
|
import { ReactNode } from 'react';
|
|
import { RouteGuard } from './RouteGuard';
|
|
|
|
interface AuthGuardProps {
|
|
children: ReactNode;
|
|
/**
|
|
* Path to redirect to if not authenticated
|
|
*/
|
|
redirectPath?: string;
|
|
/**
|
|
* Custom loading component (optional)
|
|
*/
|
|
loadingComponent?: ReactNode;
|
|
/**
|
|
* Custom unauthorized component (optional)
|
|
*/
|
|
unauthorizedComponent?: ReactNode;
|
|
}
|
|
|
|
/**
|
|
* AuthGuard Component
|
|
*
|
|
* Protects child components requiring authentication.
|
|
*
|
|
* Usage:
|
|
* ```tsx
|
|
* <AuthGuard>
|
|
* <ProtectedPage />
|
|
* </AuthGuard>
|
|
* ```
|
|
*/
|
|
export function AuthGuard({
|
|
children,
|
|
redirectPath = '/auth/login',
|
|
loadingComponent,
|
|
unauthorizedComponent,
|
|
}: AuthGuardProps) {
|
|
return (
|
|
<RouteGuard
|
|
config={{
|
|
requiredRoles: [], // Any authenticated user
|
|
redirectOnUnauthorized: true,
|
|
unauthorizedRedirectPath: redirectPath,
|
|
}}
|
|
loadingComponent={loadingComponent}
|
|
unauthorizedComponent={unauthorizedComponent}
|
|
>
|
|
{children}
|
|
</RouteGuard>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* useAuth Hook
|
|
*
|
|
* Simplified hook for checking authentication status.
|
|
*
|
|
* Usage:
|
|
* ```tsx
|
|
* const { isAuthenticated, loading } = useAuth();
|
|
* ```
|
|
*/
|
|
export { useRouteGuard as useAuthAccess } from './RouteGuard'; |