Saltearse al contenido

Integración con Firebase Auth

Esta guía muestra un patrón simple y sólido para usar Protected con Firebase Auth.

Ventana de terminal
pnpm add @arielgonzaguer/michi-router firebase

Protected necesita un objeto con:

{
user: User | null;
isLoading: boolean;
}
import React, { useEffect } from 'react';
import { RouterProvider } from '@arielgonzaguer/michi-router/core';
import { Protected } from '@arielgonzaguer/michi-router/protected';
import useAuthStore from './store/useAuthStore';
import Home from './Home';
import Login from './Login';
import Dashboard from './Dashboard';
import NotFound from './NotFound';
export default function AppRouter() {
const auth = useAuthStore();
useEffect(() => {
auth.initializeAuth();
}, [auth]);
const protectedConfig = {
states: { user: auth.user, isLoading: auth.isLoading },
redirectionPath: '/login',
loadingComponent: <div>Verificando autenticación...</div>
};
const routes = [
{ path: '/', component: <Home /> },
{ path: '/login', component: <Login /> },
{
path: '/dashboard',
component: (
<Protected configObject={protectedConfig}>
<Dashboard />
</Protected>
)
}
];
return <RouterProvider routes={routes} notFound={<NotFound />} />;
}
  • Inicialice isLoading en true hasta que termine onAuthStateChanged.
  • Mantenga redirectionPath interna (/login, /).
  • Aplique autorización real en backend/Firestore rules.

Protected protege la UI en cliente.
Para datos sensibles, valide permisos en backend o reglas de Firestore.