Implement logout

Also introduce a library to handle cookies
This commit is contained in:
Valentin Tolmer
2021-05-18 19:04:06 +02:00
parent d57cd1230c
commit 4d9f554fe6
5 changed files with 150 additions and 70 deletions

54
app/src/logout.rs Normal file
View File

@@ -0,0 +1,54 @@
use crate::cookies::delete_cookie;
use yew::prelude::*;
use yew::services::ConsoleService;
pub struct LogoutButton {
link: ComponentLink<Self>,
on_logged_out: Callback<()>,
}
#[derive(Clone, PartialEq, Properties)]
pub struct Props {
pub on_logged_out: Callback<()>,
}
pub enum Msg {
Logout,
}
impl Component for LogoutButton {
type Message = Msg;
type Properties = Props;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
LogoutButton {
link: link.clone(),
on_logged_out: props.on_logged_out,
}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Logout => match delete_cookie("user_id") {
Err(e) => {
ConsoleService::error(&e.to_string());
false
}
Ok(()) => {
self.on_logged_out.emit(());
true
}
},
}
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
html! {
<button onclick=self.link.callback(|_| { Msg::Logout })>{"Logout"}</button>
}
}
}