use super::missing_context;
use crate::{
context::{Authentication, OAuth2Context},
hook::use_auth_state,
};
use std::rc::Rc;
use yew::prelude::*;
pub trait UseAuthenticationProperties: Clone {
fn set_authentication(&mut self, auth: Authentication);
}
#[derive(Clone, Debug, Properties)]
pub struct UseAuthenticationComponentProperties<C>
where
C: BaseComponent,
C::Properties: UseAuthenticationProperties,
{
pub children: ChildrenWithProps<C>,
}
impl<C> PartialEq for UseAuthenticationComponentProperties<C>
where
C: BaseComponent,
C::Properties: UseAuthenticationProperties,
{
fn eq(&self, other: &Self) -> bool {
self.children == other.children
}
}
#[component(UseAuthentication)]
pub fn use_authentication<C>(props: &UseAuthenticationComponentProperties<C>) -> Html
where
C: BaseComponent,
C::Properties: UseAuthenticationProperties,
{
let auth = use_auth_state();
html!(
if let Some(auth) = auth {
if let OAuth2Context::Authenticated(auth) = auth {
{ for props.children.iter().map(|mut c|{
let props = Rc::make_mut(&mut c.props);
props.set_authentication(auth.clone());
c
}) }
}
} else {
{ missing_context() }
}
)
}