encode user as base64 and store in localstorage

This commit is contained in:
Yash Karandikar 2022-02-10 11:43:33 -06:00
parent 8acde0f535
commit 7d48ae6eea
5 changed files with 43 additions and 5 deletions

8
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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"] }

View file

@ -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<G> {
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"));
}));
});

6
app/src/local_storage.rs Normal file
View file

@ -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<String>);