Login Session/ Logout button
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
use crate::Route;
|
use crate::{contexts::AuthContext, Route};
|
||||||
|
|
||||||
const NAV_CSS: Asset = asset!("/assets/style/nav.scss");
|
const NAV_CSS: Asset = asset!("/assets/style/nav.scss");
|
||||||
const NAV_ICON: Asset = asset!("/assets/favicon.ico");
|
const NAV_ICON: Asset = asset!("/assets/favicon.ico");
|
||||||
@@ -47,7 +47,14 @@ pub fn Widgets() -> Element {
|
|||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn Utils() -> Element {
|
pub fn Utils() -> Element {
|
||||||
|
let mut auth_context = use_context::<AuthContext>();
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
div { class: "utils" }
|
div { class: "utils",
|
||||||
|
div{
|
||||||
|
onclick: move |_| auth_context.logout(),
|
||||||
|
"Logout"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use dioxus::{html::u::icon, prelude::*};
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
use crate::components::icons;
|
use crate::components::icons;
|
||||||
|
|
||||||
|
|||||||
@@ -7,12 +7,18 @@ pub struct AuthContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AuthContext {
|
impl AuthContext {
|
||||||
pub fn set_token(&mut self, token: String) {
|
pub fn login(&mut self, token: String) {
|
||||||
self.jwt.set(Some(token.clone()));
|
self.jwt.set(Some(token.clone()));
|
||||||
let local_storage = window().unwrap().local_storage().unwrap().unwrap();
|
let local_storage = window().unwrap().local_storage().unwrap().unwrap();
|
||||||
_ = local_storage.set_item("token", token.as_str());
|
_ = local_storage.set_item("token", token.as_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn logout(&mut self) {
|
||||||
|
self.jwt.set(None);
|
||||||
|
let local_storage = window().unwrap().local_storage().unwrap().unwrap();
|
||||||
|
_ = local_storage.remove_item("token");
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
println!("new");
|
println!("new");
|
||||||
let local_storage = window().unwrap().local_storage().unwrap().unwrap();
|
let local_storage = window().unwrap().local_storage().unwrap().unwrap();
|
||||||
|
|||||||
@@ -36,16 +36,16 @@ pub fn Login() -> Element {
|
|||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
match res.into_inner().result.unwrap() {
|
match res.into_inner().result.unwrap() {
|
||||||
crate::rpc::aoba::login_response::Result::Jwt(jwt) => {
|
crate::rpc::aoba::login_response::Result::Jwt(jwt) => {
|
||||||
auth_context.jwt.set(Some(jwt.token));
|
auth_context.login(jwt.token);
|
||||||
}
|
}
|
||||||
crate::rpc::aoba::login_response::Result::Error(login_error) => {
|
crate::rpc::aoba::login_response::Result::Error(login_error) => {
|
||||||
auth_context.jwt.set(None);
|
auth_context.logout();
|
||||||
error.set(Some(login_error.message));
|
error.set(Some(login_error.message));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Err(_err) => {
|
Err(_err) => {
|
||||||
auth_context.jwt.set(None);
|
auth_context.logout();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user