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

@ -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<LoginComponent>,
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 => {

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;