Replies: 1 comment
-
Perhaps you could use the approach yew-oauth2 takes and put the validation logic in an intermediate component? tchatchers router.rs does something like this with auth_guard.rs: /// Function used to switch the main component's view.
pub fn switch(route: Route) -> Html {
match route {
Route::JoinRoom => html! { <AuthGuard<JoinRoomHOC> /> }, // ◄────────────
Route::Room { room } => html! { <AuthGuard<FeedHOC> {room} /> }, // ◄────
Route::SignIn => html! { <SignInHOC /> },
Route::SignUp => html! { <SignUp /> },
Route::Settings => html! { <AuthGuard<SettingsHOC> /> }, // ◄────────────
Route::LogOut => html! { <LogOut /> },
Route::NotFound => html! { <NotFound />},
}
} #[function_component(AuthGuard)]
pub fn auth_guard<T>(props: &<T as yew::BaseComponent>::Properties) -> Html
where
T: BaseComponent,
<T as yew::BaseComponent>::Properties: Clone,
{
let client_context = use_context::<Rc<ClientContext>>().expect("No app context");
let navigator = use_navigator().unwrap();
if client_context.user_context.is_some() { // ◄────────────────────────────
html! { <T ..props.clone() /> }
} else {
navigator.replace(&Route::SignIn);
ToastBus::dispatcher().send(Alert {
is_success: false,
content: "Please authenticate prior accessing the app functionnalities.".into(),
});
html! {<></>}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a Router, but I want validate in the state the token, how can I do it?
How can I access the status to validate it?
Beta Was this translation helpful? Give feedback.
All reactions