This commit is contained in:
Marko Korhonen 2020-05-01 21:26:22 +03:00
parent 43a9beaef8
commit dc595a4d87
No known key found for this signature in database
GPG key ID: 911B85FBC6003FE5
7 changed files with 50 additions and 10 deletions

View file

@ -81,6 +81,7 @@ pub async fn login(
let token = encode_token(user.id, &user.username, user.admin).map_err(|e| match e { let token = encode_token(user.id, &user.username, user.admin).map_err(|e| match e {
_ => HttpResponse::InternalServerError().finish(), _ => HttpResponse::InternalServerError().finish(),
})?; })?;
println!("Token: {}", token);
id.remember(String::from(token)); id.remember(String::from(token));
Ok(HttpResponse::Ok().json(user)) Ok(HttpResponse::Ok().json(user))

View file

@ -305,6 +305,7 @@ dependencies = [
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)",
"stdweb 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", "stdweb 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)",
"thiserror 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"web_logger 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "web_logger 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"yew 0.14.3 (registry+https://github.com/rust-lang/crates.io-index)", "yew 0.14.3 (registry+https://github.com/rust-lang/crates.io-index)",
"yew-router 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "yew-router 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -312,15 +313,15 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.15" version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"thiserror-impl 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", "thiserror-impl 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.15" version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
@ -417,7 +418,7 @@ dependencies = [
"serde_json 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)",
"slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"stdweb 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", "stdweb 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)",
"thiserror 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", "thiserror 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"wasm-bindgen 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)", "wasm-bindgen 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)",
"yew-macro 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", "yew-macro 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -515,8 +516,8 @@ dependencies = [
"checksum stdweb-internal-macros 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "58fa5ff6ad0d98d1ffa8cb115892b6e69d67799f6763e162a1c9db421dc22e11" "checksum stdweb-internal-macros 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "58fa5ff6ad0d98d1ffa8cb115892b6e69d67799f6763e162a1c9db421dc22e11"
"checksum stdweb-internal-runtime 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" "checksum stdweb-internal-runtime 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0"
"checksum syn 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03" "checksum syn 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03"
"checksum thiserror 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "54b3d3d2ff68104100ab257bb6bb0cb26c901abe4bd4ba15961f3bf867924012" "checksum thiserror 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "d12a1dae4add0f0d568eebc7bf142f145ba1aa2544cafb195c76f0f409091b60"
"checksum thiserror-impl 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972988113b7715266f91250ddb98070d033c62a011fa0fcc57434a649310dd" "checksum thiserror-impl 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "3f34e0c1caaa462fd840ec6b768946ea1e7842620d94fe29d5b847138f521269"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
"checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" "checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce"
"checksum wasm-bindgen 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)" = "2cc57ce05287f8376e998cbddfb4c8cb43b84a7ec55cf4551d7c00eef317a47f" "checksum wasm-bindgen 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)" = "2cc57ce05287f8376e998cbddfb4c8cb43b84a7ec55cf4551d7c00eef317a47f"

View file

@ -14,6 +14,7 @@ log = "0.4.8"
web_logger = "0.2.0" web_logger = "0.2.0"
serde_json = "1.0.52" serde_json = "1.0.52"
anyhow = "1.0.28" anyhow = "1.0.28"
thiserror = "1.0.16"
[[bin]] [[bin]]
name = "app" name = "app"

View file

@ -1,11 +1,10 @@
use crate::utils::cookie;
use log::{error, info}; use log::{error, info};
use serde_json::json; use serde_json::json;
use yew::format::{Json, Text}; use yew::format::Json;
use yew::prelude::*; use yew::prelude::*;
use yew::services::fetch::{FetchService, FetchTask, Request, Response}; use yew::services::fetch::{FetchService, FetchTask, Request, Response};
use yew::services::fetch::Mode::Cors;
pub struct LoginComponent { pub struct LoginComponent {
component_link: ComponentLink<LoginComponent>, component_link: ComponentLink<LoginComponent>,
username: String, username: String,
@ -97,7 +96,10 @@ impl Component for LoginComponent {
Msg::FetchReady(response) => { Msg::FetchReady(response) => {
self.fetching = false; self.fetching = false;
info!("Fetch complete. Body: {}", response) info!("Login successful: {}", response);
cookie::get("thesis")
.map(|cookie| info!("Cookie: {}", cookie))
.map_err(|e| error!("{}", e));
} }
Msg::FetchError => { Msg::FetchError => {

View file

View file

@ -0,0 +1,33 @@
use anyhow::Result;
use log::info;
use stdweb::{js, unstable::TryInto};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum CookieError {
#[error("No cookie found for given name")]
NotFound,
}
pub fn get(name: &str) -> Result<String> {
let cookie_strings = js! { return document.cookie };
let cookies: Vec<String> = cookie_strings.try_into()?;
cookies
.iter()
.filter_map(|x| {
let name_value: Vec<_> = x.split("=").collect();
match name_value.get(0) {
None => None,
Some(c) => {
if *c == name {
name_value.get(1).map(|x| (*x).to_owned())
} else {
None
}
}
}
})
.collect::<Vec<String>>()
.pop()
.ok_or_else(|| CookieError::NotFound.into())
}

View file

@ -0,0 +1,2 @@
pub mod api;
pub mod cookie;