|
|
@@ -0,0 +1,79 @@
|
|
|
+import React from 'react';
|
|
|
+import { useNavigate } from 'react-router-dom';
|
|
|
+import { useAuth } from '../hooks/AuthProvider';
|
|
|
+import { GuestPrompt } from './GuestPrompt';
|
|
|
+
|
|
|
+interface GuestGuardProps {
|
|
|
+ children: React.ReactNode;
|
|
|
+ action?: string;
|
|
|
+ fallback?: React.ReactNode;
|
|
|
+}
|
|
|
+
|
|
|
+export const GuestGuard: React.FC<GuestGuardProps> = ({
|
|
|
+ children,
|
|
|
+ action = '继续访问',
|
|
|
+ fallback
|
|
|
+}) => {
|
|
|
+ const { isAuthenticated, isLoading } = useAuth();
|
|
|
+ const navigate = useNavigate();
|
|
|
+
|
|
|
+ if (isLoading) {
|
|
|
+ return (
|
|
|
+ <div className="flex justify-center items-center h-screen">
|
|
|
+ <div className="loader ease-linear rounded-full border-4 border-t-4 border-gray-200 h-12 w-12"></div>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isAuthenticated) {
|
|
|
+ if (fallback) {
|
|
|
+ return fallback;
|
|
|
+ }
|
|
|
+
|
|
|
+ return (
|
|
|
+ <>
|
|
|
+ <div className="min-h-screen bg-gray-50 flex items-center justify-center">
|
|
|
+ <div className="text-center">
|
|
|
+ <div className="w-20 h-20 bg-blue-100 rounded-full flex items-center justify-center mx-auto mb-4">
|
|
|
+ <svg
|
|
|
+ className="w-10 h-10 text-blue-600"
|
|
|
+ fill="none"
|
|
|
+ stroke="currentColor"
|
|
|
+ viewBox="0 0 24 24"
|
|
|
+ >
|
|
|
+ <path
|
|
|
+ strokeLinecap="round"
|
|
|
+ strokeLinejoin="round"
|
|
|
+ strokeWidth={2}
|
|
|
+ d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2-2v6a2 2 0 002 2zm10-10V7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"
|
|
|
+ />
|
|
|
+ </svg>
|
|
|
+ </div>
|
|
|
+ <h2 className="text-xl font-semibold text-gray-900 mb-2">
|
|
|
+ 需要登录才能{action}
|
|
|
+ </h2>
|
|
|
+ <p className="text-gray-600 mb-6">
|
|
|
+ 登录或注册以享受完整功能
|
|
|
+ </p>
|
|
|
+ <div className="space-y-3 max-w-xs mx-auto">
|
|
|
+ <button
|
|
|
+ onClick={() => navigate('/login')}
|
|
|
+ className="w-full bg-blue-600 text-white py-3 px-4 rounded-lg font-medium hover:bg-blue-700 transition-colors"
|
|
|
+ >
|
|
|
+ 立即登录
|
|
|
+ </button>
|
|
|
+ <button
|
|
|
+ onClick={() => navigate('/register')}
|
|
|
+ className="w-full bg-white text-blue-600 py-3 px-4 rounded-lg font-medium border border-blue-600 hover:bg-blue-50 transition-colors"
|
|
|
+ >
|
|
|
+ 注册账号
|
|
|
+ </button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </>
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ return <>{children}</>;
|
|
|
+};
|