40 lines
832 B
TypeScript
40 lines
832 B
TypeScript
/**
|
|
* Route Guard Component
|
|
* 路由守卫组件 - 处理认证和授权
|
|
*/
|
|
|
|
import { Navigate } from "react-router-dom";
|
|
import type { ReactNode } from "react";
|
|
|
|
interface RouteGuardProps {
|
|
isAuthenticated: boolean;
|
|
redirectTo?: string;
|
|
children: ReactNode;
|
|
}
|
|
|
|
/**
|
|
* Protected route wrapper
|
|
* Redirects to auth page if not authenticated
|
|
*/
|
|
export const ProtectedRoute = ({
|
|
isAuthenticated,
|
|
redirectTo = "/",
|
|
children
|
|
}: RouteGuardProps) => {
|
|
return isAuthenticated ? <>{children}</> : <Navigate to={redirectTo} replace />;
|
|
};
|
|
|
|
/**
|
|
* Public route wrapper
|
|
* Redirects to home if already authenticated
|
|
*/
|
|
export const PublicRoute = ({
|
|
isAuthenticated,
|
|
redirectTo = "/home",
|
|
children
|
|
}: RouteGuardProps) => {
|
|
return !isAuthenticated ? <>{children}</> : <Navigate to={redirectTo} replace />;
|
|
};
|
|
|
|
|