yew_oauth2/components/redirect/
router.rs

1//! Redirect by pushing a new [`yew_nested_router::prelude::Target`].
2
3use super::{Redirect, Redirector, RedirectorProperties};
4use yew::prelude::*;
5use yew_nested_router::prelude::*;
6
7/// A redirector using Yew's Router, and the Browser's History API.
8pub struct RouterRedirector<R>
9where
10    R: Target + 'static,
11{
12    router: Option<RouterContext<R>>,
13    _handle: Option<ContextHandle<RouterContext<R>>>,
14}
15
16impl<R> Redirector for RouterRedirector<R>
17where
18    R: Target + 'static,
19{
20    type Properties = RouterProperties<R>;
21
22    fn new<COMP: Component>(ctx: &Context<COMP>) -> Self {
23        // while the "route" can change, the "router" itself does not.
24        let cb = Callback::from(|_| {});
25        let (router, handle) = match ctx.link().context::<RouterContext<R>>(cb) {
26            Some((router, handle)) => (Some(router), Some(handle)),
27            None => (None, None),
28        };
29
30        Self {
31            router,
32            _handle: handle,
33        }
34    }
35
36    fn logout(&self, props: &Self::Properties) {
37        let route = props.logout.clone();
38        log::debug!("ChangeRoute due to logout: {:?}", route);
39
40        if let Some(router) = &self.router {
41            router.push(route);
42        }
43    }
44}
45
46/// Properties for the [`RouterRedirector`] component.
47#[derive(Clone, Debug, PartialEq, Properties)]
48pub struct RouterProperties<R>
49where
50    R: Target + 'static,
51{
52    #[prop_or_default]
53    pub children: Html,
54    pub logout: R,
55}
56
57impl<R> RedirectorProperties for RouterProperties<R>
58where
59    R: Target + 'static,
60{
61    fn children(&self) -> &Html {
62        &self.children
63    }
64}
65
66pub mod oauth2 {
67    //! Convenient access for the OAuth2 variant
68    use super::*;
69    use crate::agent::client::OAuth2Client;
70    pub type RouterRedirect<R> = Redirect<OAuth2Client, RouterRedirector<R>>;
71}
72
73#[cfg(feature = "openid")]
74pub mod openid {
75    //! Convenient access for the Open ID Connect variant
76    use super::*;
77    use crate::agent::client::OpenIdClient;
78    pub type RouterRedirect<R> = Redirect<OpenIdClient, RouterRedirector<R>>;
79}