orchetera
Version:
Welcome to **Orchetera** — your orchestration tool to kickstart Firebase-ready projects with ease!
24 lines (19 loc) • 617 B
JSX
// components/AuthGuard.jsx
import { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { getAuth, onAuthStateChanged } from "firebase/auth";
export default function AuthGuard({ children }) {
const navigate = useNavigate();
const auth = getAuth();
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
if (!user) {
// User is not authenticated, redirect to login
navigate("/login");
}
});
return () => unsubscribe();
}, [auth, navigate]);
// Optional: Show loading state while checking auth
return children;
}