You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure if this is the best way to do it but I use a nested route that acts as a user guard:
/* Assuming useUser is a signal indicating whether the user is logged in or not */constUserGuard: Component=()=>{constnavigate=useNavigate();/* Authentication guard */createEffect(()=>{/* Here null means user is not logged in. It could also be undefined which means we don't know yet.*/if(useUser()===null){navigate("/login",{replace: true});}});return(<Showwhen={useUser()}fallback="Loading user..."><Outlet/></Show>);}
It displays loading user if the user signal is undefined and redirects if it is null (as in the user is not logged in). If they are logged in then it shows the child routes that are guarded. I then use it in the Routes:
Activity
nuric commentedon Jul 28, 2022
I'm not sure if this is the best way to do it but I use a nested route that acts as a user guard:
It displays loading user if the user signal is undefined and redirects if it is null (as in the user is not logged in). If they are logged in then it shows the child routes that are guarded. I then use it in the Routes: