inogen/app/components/auth/auth-guard.tsx

35 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React from "react";
import { useAuth } from "~/contexts/auth-context";
import { LoginForm } from "./login-form";
import toast from "react-hot-toast";
interface AuthGuardProps {
children: React.ReactNode;
}
export function AuthGuard({ children }: AuthGuardProps) {
const { isAuthenticated, isLoading, user, token } = useAuth();
// Show loading while checking authentication
if (isLoading) {
return (
<div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-slate-50 to-slate-100 dark:from-slate-900 dark:to-slate-800">
<div className="text-center">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-green-500 mx-auto mb-4"></div>
<p className="text-gray-600 dark:text-gray-400 font-persian">
در حال بررسی احراز هویت...
</p>
</div>
</div>
);
}
// If user is not authenticated or token is invalid, show login form
if (!isAuthenticated || !token || !token.accessToken) {
return <LoginForm />;
}
// If user is authenticated, show the protected content
return <>{children}</>;
}