-
Hi ! In my case if I go to /users - I still see that local resource inside were triggered even if I do not loggedin. I try different approaches with and with no luck. Is this expected behavior ? Parent route act as guard on each page change - check if token is still there and not expired (no extra calls to API for now) fn App() -> impl IntoView {
let store = Store::new(UserData::default()).from_local();
provide_context(store);
view! {
<Router>
<Routes fallback=|| "Not found">
<Route path=path!("/login") view=Login />
<ParentRoute
path=path!("/")
view=move || {
let location = use_location();
let navigate = leptos_router::hooks::use_navigate();
Effect::new(move || {
let _ = location.pathname.get();
if !store .token_is_valid() {
navigate("/login", Default::default());
}
});
view! {
<Navbar>
<Outlet />
</Navbar>
}
}
>
// Nested routes
<ParentRoute path=path!("/users") view=UsersList>
<Route path=path!("/create") view=UserCreate />
<Route path=path!(":id") view=UserProfile />
<Route path=path!("") view=UserNotSelected />
</ParentRoute> |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Based on the way you have written this code, this is expected behavior: You should consider using |
Beta Was this translation helpful? Give feedback.
Based on the way you have written this code, this is expected behavior:
Effect
runs for the first time on the next "tick," after the page is rendered and mounted.You should consider using
ProtectedParentRoute
for this behavior, orremoving. (I see you want it to run whenever the pathname changes, which make sense)Effect::new()
so that the code to check the route runs immediately