diff --git a/Cargo.lock b/Cargo.lock index 0b25003..1ceeae5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,6 +35,7 @@ dependencies = [ name = "app" version = "0.1.0" dependencies = [ + "base64", "console_error_panic_hook", "fancy-regex", "getrandom", @@ -114,6 +115,12 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b" +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + [[package]] name = "base64ct" version = "1.0.1" @@ -161,6 +168,7 @@ name = "bugspray" version = "0.1.0" dependencies = [ "app", + "base64", "lazy_static", "rocket", "rusqlite", diff --git a/Cargo.toml b/Cargo.toml index 42c4010..f767ddb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] app = { path = "./app" } +base64 = "0.13.0" lazy_static = "1.4.0" rocket = { version = "0.5.0-rc.1", features = ["json"] } rusqlite = "0.26.3" diff --git a/app/Cargo.toml b/app/Cargo.toml index d759f26..617f29b 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +base64 = "0.13.0" console_error_panic_hook = "0.1.7" fancy-regex = "0.7.1" getrandom = { version = "0.2.4", features = ["js"] } diff --git a/app/src/lib.rs b/app/src/lib.rs index 2013f26..48c9d09 100644 --- a/app/src/lib.rs +++ b/app/src/lib.rs @@ -1,3 +1,5 @@ +mod local_storage; + use fancy_regex::Regex; use pbkdf2::password_hash::PasswordVerifier; use reqwasm::http::Request; @@ -8,11 +10,7 @@ use sycamore_router::{ }; use wasm_bindgen::prelude::*; -#[wasm_bindgen] -extern "C" { - #[wasm_bindgen(js_namespace = console)] - fn log(s: &str); -} +use base64::encode; macro_rules! wasm_import { ($($tt:tt)*) => { @@ -24,7 +22,19 @@ macro_rules! wasm_import { }; } +#[macro_export] +macro_rules! wasm_import_with_ns { + ($ns: ident, $($tt:tt)*) => { + #[wasm_bindgen] + extern "C" { + #[wasm_bindgen(js_namespace = $ns)] + pub fn $($tt)*; + } + }; +} + wasm_import!(setLocation(href: &str)); +wasm_import_with_ns!(console, log(s: &str)); #[derive(PartialEq, Eq, Debug, Default, Clone, Serialize, Deserialize)] pub struct Issue { @@ -398,6 +408,18 @@ pub fn login() -> View { error_wrong_password.set(Pbkdf2.verify_password(password_raw.as_bytes(), &hashed).is_err()); + if *error_wrong_password.get() { + text.set(String::from("Log in")); + return; + } + + let encoded = encode(serde_json::to_string(&user).unwrap().as_bytes()); + + local_storage::setItem("token", &encoded); + log(&encoded); + + setLocation("/"); + text.set(String::from("Log in")); })); }); diff --git a/app/src/local_storage.rs b/app/src/local_storage.rs new file mode 100644 index 0000000..b85c193 --- /dev/null +++ b/app/src/local_storage.rs @@ -0,0 +1,6 @@ +use wasm_bindgen::prelude::*; + +use crate::wasm_import_with_ns; + +wasm_import_with_ns!(localStorage, setItem(key: &str, value: &str)); +wasm_import_with_ns!(localStorage, getItem(key: &str) -> Option);