mirror of
https://github.com/lldap/lldap.git
synced 2026-07-25 17:58:44 +00:00
app: implement login and authorization
This commit is contained in:
+51
-7
@@ -1,20 +1,60 @@
|
||||
use crate::login::LoginForm;
|
||||
use crate::user_table::UserTable;
|
||||
use anyhow::{anyhow, Result};
|
||||
use wasm_bindgen::JsCast;
|
||||
use yew::prelude::*;
|
||||
|
||||
pub struct App {}
|
||||
pub struct App {
|
||||
link: ComponentLink<Self>,
|
||||
user_name: Option<String>,
|
||||
}
|
||||
|
||||
pub enum Msg {}
|
||||
pub enum Msg {
|
||||
Login(String),
|
||||
}
|
||||
|
||||
fn extract_user_id_cookie() -> Result<String> {
|
||||
let document = web_sys::window()
|
||||
.unwrap()
|
||||
.document()
|
||||
.unwrap()
|
||||
.dyn_into::<web_sys::HtmlDocument>()
|
||||
.unwrap();
|
||||
let cookies = document.cookie().unwrap();
|
||||
yew::services::ConsoleService::info(&cookies);
|
||||
cookies
|
||||
.split(";")
|
||||
.filter_map(|c| c.split_once('='))
|
||||
.map(|(name, value)| {
|
||||
if name == "user_id" {
|
||||
Ok(value.into())
|
||||
} else {
|
||||
Err(anyhow!("Wrong cookie"))
|
||||
}
|
||||
})
|
||||
.filter(Result::is_ok)
|
||||
.next()
|
||||
.unwrap_or(Err(anyhow!("User ID cookie not found in response")))
|
||||
}
|
||||
|
||||
impl Component for App {
|
||||
type Message = Msg;
|
||||
type Properties = ();
|
||||
|
||||
fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
|
||||
App {}
|
||||
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||
App {
|
||||
link: link.clone(),
|
||||
user_name: extract_user_id_cookie().ok(),
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, _msg: Self::Message) -> ShouldRender {
|
||||
false
|
||||
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
||||
match msg {
|
||||
Msg::Login(user_name) => {
|
||||
self.user_name = Some(user_name);
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
||||
@@ -25,7 +65,11 @@ impl Component for App {
|
||||
html! {
|
||||
<div>
|
||||
<h1>{ "LLDAP" }</h1>
|
||||
<UserTable />
|
||||
{if self.user_name.is_some() {
|
||||
html! {<UserTable />}
|
||||
} else {
|
||||
html! {<LoginForm on_logged_in=self.link.callback(|u| { Msg::Login(u) })/>}
|
||||
}}
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user