From 2d2ef65d22c54169759568bc8909492d24b0ca81 Mon Sep 17 00:00:00 2001 From: skye Date: Thu, 27 Apr 2023 15:20:04 +0300 Subject: [PATCH] fix(client): change where user token is saved --- client/src/api/user/login.rs | 34 +++---------------- client/src/api/user/mod.rs | 9 +++++ client/src/macros/async_task.rs | 24 ++++++++++++- .../plugins/async_tasks/req_game_create.rs | 6 ++-- .../plugins/async_tasks/req_game_forming.rs | 2 +- .../plugins/async_tasks/req_game_mygames.rs | 2 +- client/src/plugins/async_tasks/req_login.rs | 5 +-- .../src/plugins/async_tasks/req_register.rs | 2 +- 8 files changed, 46 insertions(+), 38 deletions(-) diff --git a/client/src/api/user/login.rs b/client/src/api/user/login.rs index ec9fb7b..128a576 100644 --- a/client/src/api/user/login.rs +++ b/client/src/api/user/login.rs @@ -6,33 +6,13 @@ * See LICENSE for licensing information. */ -use serde::{Deserialize, Serialize}; +use serde::Serialize; use crate::{NetworkingOptions, post_request}; -use super::User; +use super::UserToken; -#[derive(Deserialize)] -struct LoginResponseJson { - id: String, - created_at: String, - updated_at: String, - username: String, - token: String, -} - -impl LoginResponseJson { - fn to_user(&self) -> User { - User { - id: self.id.clone(), - created_at: self.created_at.clone(), - updated_at: self.updated_at.clone(), - username: self.username.clone(), - } - } -} - -pub type LoginResponse = Result; +pub type LoginResponse = Result; #[derive(Serialize)] struct LoginPost { @@ -40,15 +20,11 @@ struct LoginPost { password: String, } -pub fn login(no: &mut NetworkingOptions, email: &str, password: &str) -> LoginResponse { +pub fn login(no: &NetworkingOptions, email: &str, password: &str) -> LoginResponse { let res = post_request!(no, "/user/token", &LoginPost { email: email.to_string(), password: password.to_string(), }); - let tmp: LoginResponseJson = res.json().unwrap(); - - no.user_token = tmp.token.clone(); - - Ok(tmp.to_user()) + Ok(res.json().unwrap()) } diff --git a/client/src/api/user/mod.rs b/client/src/api/user/mod.rs index 8557e10..233b822 100644 --- a/client/src/api/user/mod.rs +++ b/client/src/api/user/mod.rs @@ -21,3 +21,12 @@ pub struct User { pub updated_at: String, pub username: String, } + +#[derive(Deserialize)] +pub struct UserToken { + pub id: String, + pub created_at: String, + pub updated_at: String, + pub username: String, + pub token: String, +} diff --git a/client/src/macros/async_task.rs b/client/src/macros/async_task.rs index 48ac443..e907b6c 100644 --- a/client/src/macros/async_task.rs +++ b/client/src/macros/async_task.rs @@ -33,7 +33,7 @@ macro_rules! async_task_start_call { for ev in start_ev_r.iter() { let thread_pool = AsyncComputeTaskPool::get(); let $ev = ev.clone(); - let mut $no = no.clone(); + let $no = no.clone(); let task = thread_pool.spawn(async move { $func }); @@ -88,4 +88,26 @@ macro_rules! async_task_handle_call { } }; + ($call_type:ty, |$response:ident, $menu_data:ident, $no:ident| $handler_func:expr) => { + pub fn handle_call( + mut commands: Commands, + mut tasks: Query<(Entity, &mut $call_type)>, + mut $menu_data: ResMut, + mut $no: ResMut, + ) { + match tasks.get_single_mut() { + Ok((entity, mut task)) => { + if let Some($response) = future::block_on(future::poll_once(&mut task.0)) { + $handler_func; + + // remove the task + commands.entity(entity).remove::<$call_type>(); + commands.entity(entity).despawn_recursive(); + } + } + // NOTE: don't do anything if the wanted thingy doesn't exist + _ => {} + } + } + }; } diff --git a/client/src/plugins/async_tasks/req_game_create.rs b/client/src/plugins/async_tasks/req_game_create.rs index 9e5beb8..545ffe3 100644 --- a/client/src/plugins/async_tasks/req_game_create.rs +++ b/client/src/plugins/async_tasks/req_game_create.rs @@ -21,13 +21,13 @@ pub struct GameCreateCall(Task); #[derive(Clone)] pub struct GameCreateCallEvent; -async_task_start_call!(GameCreateCallEvent, GameCreateCall, |ev, no| { - let res = api::game::create(&mut no); +async_task_start_call!(GameCreateCallEvent, GameCreateCall, |_ev, no| { + let res = api::game::create(&no); res }); -async_task_handle_call!(GameCreateCall, |response, menu_data| { +async_task_handle_call!(GameCreateCall, |response, _menu_data| { match response { Err(_err) => panic!("game create failed, handle me"), Ok(resp) => { diff --git a/client/src/plugins/async_tasks/req_game_forming.rs b/client/src/plugins/async_tasks/req_game_forming.rs index a3561b5..ebf8639 100644 --- a/client/src/plugins/async_tasks/req_game_forming.rs +++ b/client/src/plugins/async_tasks/req_game_forming.rs @@ -22,7 +22,7 @@ pub struct GameFormingCall(Task); pub struct GameFormingCallEvent; async_task_start_call!(GameFormingCallEvent, GameFormingCall, |_ev, no| { - let res = api::game::forming(&mut no); + let res = api::game::forming(&no); res }); diff --git a/client/src/plugins/async_tasks/req_game_mygames.rs b/client/src/plugins/async_tasks/req_game_mygames.rs index 450cc75..b41b0f4 100644 --- a/client/src/plugins/async_tasks/req_game_mygames.rs +++ b/client/src/plugins/async_tasks/req_game_mygames.rs @@ -22,7 +22,7 @@ pub struct GameMyGamesCall(Task); pub struct GameMyGamesCallEvent; async_task_start_call!(GameMyGamesCallEvent, GameMyGamesCall, |_ev, no| { - let res = api::game::my_games(&mut no); + let res = api::game::my_games(&no); res }); diff --git a/client/src/plugins/async_tasks/req_login.rs b/client/src/plugins/async_tasks/req_login.rs index 0d25a12..1487597 100644 --- a/client/src/plugins/async_tasks/req_login.rs +++ b/client/src/plugins/async_tasks/req_login.rs @@ -25,16 +25,17 @@ pub struct LoginCallEvent { } async_task_start_call!(LoginCallEvent, LoginCall, |ev, no| { - let res = api::user::login(&mut no, &ev.email, &ev.password); + let res = api::user::login(&no, &ev.email, &ev.password); res }); -async_task_handle_call!(LoginCall, |response, menu_data| { +async_task_handle_call!(LoginCall, |response, menu_data, no| { match response { Err(_err) => panic!("login failed, handle me"), Ok(resp) => { info!("logged in {}", resp.username); + no.user_token = resp.token; menu_data.ui_state = MenuUIState::Main; } } diff --git a/client/src/plugins/async_tasks/req_register.rs b/client/src/plugins/async_tasks/req_register.rs index 7836dbf..bcad3ee 100644 --- a/client/src/plugins/async_tasks/req_register.rs +++ b/client/src/plugins/async_tasks/req_register.rs @@ -26,7 +26,7 @@ pub struct RegisterCallEvent { } async_task_start_call!(RegisterCallEvent, RegisterCall, |ev, no| { - let res = api::user::register(&mut no, &ev.username, &ev.email, &ev.password); + let res = api::user::register(&no, &ev.username, &ev.email, &ev.password); res }); -- 2.44.1