New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Router Component Authentication #1526
Comments
Did you have any luck with this? I was wanting to setup something similar. Coming from React Router v4+ I wanted something more like what I do with that. I've been experimenting with it. I'm not sure if this is better than what you have using
I was wanting something that was very declarative and worked with the Router component in a way that all components do. The problem I found with the Here's a link to the |
What you have there is probably the best workaround right now IIRC. The way i was talking about was forgoing the router component all together and just passing a custom callback to the route service provider (witch the router component uses internally) to change route's based on the provided route, it really is to much work for what it's worth. |
@hamza1311 do you have anything to add to this? Otherwise I would suggest we move this over to a discussion (now that that's a thing) or create a new feature request that's more specific about the details. |
@siku2, I don't think we should add this. Every time I used this in another router (react and angular), it gave me nothing but trouble. The callback to check if the user is authenticated needs to be async. Consider this case: a user token is stored but it needs to be verified by the server before marking authenticated state as true in the UI. We can't do that unless the callback is async. It would be better if user handled it themselves in their component. We should, however, provide a component that redirects to given path and renders Here's an example of the pain it has caused me in the past: https://github.com/hamza1311/dear-diary-web/blob/99b070abe46288c1722414323d17cc3ef50c7dd2/src/components/Routes.tsx#L36-L61 |
Hi I see this issue is open and I think that yew router needs 2 features: Guards and Data resolver. First guards route to be access given a function ( like authenticated user ) and the second is to fetch data when route navigation is requested but to render the component after the data is received. |
I'm afraid it isn't as simple as what you describe. As I said in my previous comment, this will need async support to be practically usable. I can't think of a way to allow that without making the entire router async, which is inefficient as Promises aren't that well supported (think of lack of ability to directly get the return message from spawn_local, yew's methods being async, async not being supported in traits, etc). Maybe Suspense can be used for this. |
Yes understand, maybe in future when async methods in traits are stable. One more use case for async authentication which uses JWT tokens: User navigates to a page but the access token has been expired so a call with refresh token should be made and when access token is renewed then should be able to continue to the page. |
Just checked the Suspense I didn't know about it because it comes in the next version, this will help me in the fetching the data before the page is rendered. Thanks for the tip. |
I believe the fetch-then-render pattern is mostly manually implemented and is usually router agnostic (data is dispatched alongside route when fetched).
A // A thread_local! should be used here, static is only for demonstration purpose.
static GO_TO_LOGIN: Lazy<HtmlResult> = Lazy::new(|| Ok(html!{ <Redirect<Route> to={Route::Login} /> }));
#[function_component]
fn MyAccount() -> HtmlResult {
let user = match use_user()? {
Some(m) => m,
None => return GO_TO_LOGIN.clone(),
};
Ok(html! { <div>{"Hello, "}{user.name}</div> })
} |
we can use the follow code to auto redirect from any page to any other page use wasm_bindgen::UnwrapThrowExt;
use web_sys::window;
let document = window()
.expect_throw("window is undefined").location().assign("/test"); |
Question
I'm building a generic admin panel, I'm struggling implementing an authentication system that can work with the router component. In short it works by sending the browser a list of permissions then when a route is changed i can check this list and either allow the route change or redirect to an unauthorized error page.
Is there a way to do this with the Router component?
What I've tried (optional)
I've been able to do this by not using the router component and using RouteService::register_callback, i would just prefer to be able to use the included component.
Ideally i think it would be helpful to add a
filter
function similar to render & redirect in the router props that will map a route to another routeFn(SW) -> SW
by default this could just pass the route on unchanged and would give a place to modify the route before the change.The text was updated successfully, but these errors were encountered: