/project
This commit is contained in:
parent
43a9beaef8
commit
dc595a4d87
7 changed files with 50 additions and 10 deletions
|
@ -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 => {
|
||||
|
|
0
project/frontend/src/utils/api.rs
Normal file
0
project/frontend/src/utils/api.rs
Normal file
33
project/frontend/src/utils/cookie.rs
Normal file
33
project/frontend/src/utils/cookie.rs
Normal 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())
|
||||
}
|
2
project/frontend/src/utils/mod.rs
Normal file
2
project/frontend/src/utils/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod api;
|
||||
pub mod cookie;
|
Loading…
Add table
Add a link
Reference in a new issue