From dc595a4d87cf1579debabddc302564d25ad9b157 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Fri, 1 May 2020 21:26:22 +0300 Subject: [PATCH] /project --- .../backend/src/handlers/authentication.rs | 1 + project/frontend/Cargo.lock | 13 ++++---- project/frontend/Cargo.toml | 1 + project/frontend/src/component/login.rs | 10 +++--- project/frontend/src/utils/api.rs | 0 project/frontend/src/utils/cookie.rs | 33 +++++++++++++++++++ project/frontend/src/utils/mod.rs | 2 ++ 7 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 project/frontend/src/utils/api.rs create mode 100644 project/frontend/src/utils/cookie.rs create mode 100644 project/frontend/src/utils/mod.rs diff --git a/project/backend/src/handlers/authentication.rs b/project/backend/src/handlers/authentication.rs index fef4eb4..58ef898 100644 --- a/project/backend/src/handlers/authentication.rs +++ b/project/backend/src/handlers/authentication.rs @@ -81,6 +81,7 @@ pub async fn login( let token = encode_token(user.id, &user.username, user.admin).map_err(|e| match e { _ => HttpResponse::InternalServerError().finish(), })?; + println!("Token: {}", token); id.remember(String::from(token)); Ok(HttpResponse::Ok().json(user)) diff --git a/project/frontend/Cargo.lock b/project/frontend/Cargo.lock index 4455ff8..a9ae073 100644 --- a/project/frontend/Cargo.lock +++ b/project/frontend/Cargo.lock @@ -305,6 +305,7 @@ dependencies = [ "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)", "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)", "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)", @@ -312,15 +313,15 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.15" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "thiserror-impl" -version = "1.0.15" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "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)", "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)", - "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)", "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-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 thiserror 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "54b3d3d2ff68104100ab257bb6bb0cb26c901abe4bd4ba15961f3bf867924012" -"checksum thiserror-impl 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972988113b7715266f91250ddb98070d033c62a011fa0fcc57434a649310dd" +"checksum thiserror 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "d12a1dae4add0f0d568eebc7bf142f145ba1aa2544cafb195c76f0f409091b60" +"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 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" diff --git a/project/frontend/Cargo.toml b/project/frontend/Cargo.toml index aa06fa3..96c0f90 100644 --- a/project/frontend/Cargo.toml +++ b/project/frontend/Cargo.toml @@ -14,6 +14,7 @@ log = "0.4.8" web_logger = "0.2.0" serde_json = "1.0.52" anyhow = "1.0.28" +thiserror = "1.0.16" [[bin]] name = "app" diff --git a/project/frontend/src/component/login.rs b/project/frontend/src/component/login.rs index 9c868f2..491bb69 100644 --- a/project/frontend/src/component/login.rs +++ b/project/frontend/src/component/login.rs @@ -1,11 +1,10 @@ +use crate::utils::cookie; use log::{error, info}; use serde_json::json; -use yew::format::{Json, Text}; +use yew::format::Json; use yew::prelude::*; use yew::services::fetch::{FetchService, FetchTask, Request, Response}; -use yew::services::fetch::Mode::Cors; - pub struct LoginComponent { component_link: ComponentLink, username: String, @@ -97,7 +96,10 @@ impl Component for LoginComponent { Msg::FetchReady(response) => { 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 => { diff --git a/project/frontend/src/utils/api.rs b/project/frontend/src/utils/api.rs new file mode 100644 index 0000000..e69de29 diff --git a/project/frontend/src/utils/cookie.rs b/project/frontend/src/utils/cookie.rs new file mode 100644 index 0000000..8856428 --- /dev/null +++ b/project/frontend/src/utils/cookie.rs @@ -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 { + let cookie_strings = js! { return document.cookie }; + let cookies: Vec = 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::>() + .pop() + .ok_or_else(|| CookieError::NotFound.into()) +} diff --git a/project/frontend/src/utils/mod.rs b/project/frontend/src/utils/mod.rs new file mode 100644 index 0000000..faf7ecf --- /dev/null +++ b/project/frontend/src/utils/mod.rs @@ -0,0 +1,2 @@ +pub mod api; +pub mod cookie;