diff --git a/src/backend/data.rs b/src/backend/data.rs index 106d7b0..d527e58 100644 --- a/src/backend/data.rs +++ b/src/backend/data.rs @@ -32,3 +32,8 @@ pub enum AccountRegisterCriteriaProblemResponse { Email, Password, } + +#[derive(Deserialize)] +pub struct AccountVerifyResponse { + pub token: String, +} diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 19512ca..156ba68 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -64,16 +64,10 @@ impl Session { let response = request.send().await?; match response.status() { - 200 => { - let token = response - .json::() - .await? - .token; - - set_auth_cookie(&token); - - Ok(token) - } + 200 => Ok(response + .json::() + .await? + .token), 400 => Err(Error::msg(format!("Bad request"))), 401 => Err(Error::msg(format!("Wrong password!"))), 403 => Err(Error::msg(format!("You're not allowed to do this!"))), @@ -83,9 +77,7 @@ impl Session { } }; - wasm_bindgen_futures::spawn_local( - async move { callback.emit(call.await) }, - ); + wasm_bindgen_futures::spawn_local(async move { callback.emit(call.await) }); } pub fn register( @@ -145,7 +137,7 @@ impl Session { wasm_bindgen_futures::spawn_local(async move { callback.emit(call.await) }); } - pub fn verify(&self, token: String, callback: Callback>) { + pub fn verify(&self, token: String, callback: Callback>) { let url = format!("{}/account/verify", &self.base_url); let body = json!({ @@ -160,7 +152,10 @@ impl Session { let response = request.send().await?; match response.status() { - 200 => Ok(()), + 200 => { + let auth_token = response.json::().await?.token; + Ok(auth_token) + } 400 => Err(Error::msg(format!("Bad request"))), 403 => Err(Error::msg(format!("You're not allowed to do this!"))), 404 => Err(Error::msg(format!("Unknown Token!"))), @@ -172,6 +167,7 @@ impl Session { } pub fn set_token(&mut self, token: String) { + set_auth_cookie(&token); self.auth_token = Some(token); } diff --git a/src/topbar/guest/signup.rs b/src/topbar/guest/signup.rs index 8bbe60a..81989a8 100644 --- a/src/topbar/guest/signup.rs +++ b/src/topbar/guest/signup.rs @@ -1,4 +1,5 @@ use crate::backend::Session; +use crate::Callbacks; use anyhow::Result; use std::ops::Deref; use wasm_bindgen::JsCast; @@ -39,6 +40,7 @@ pub struct Props { #[function_component] pub fn SignUp(props: &Props) -> Html { let state = use_state(|| Data::default()); + let callbacks = use_context::().expect("Callbacks context not available!"); let session = use_context::().unwrap_or_default(); let cloned_state = state.clone(); @@ -107,20 +109,31 @@ pub fn SignUp(props: &Props) -> Html { }); let cloned_state = state.clone(); - let callback = props.close.clone(); - let on_api_response = Callback::from(move |response: Result<()>| { + let on_sign_up_api_response = Callback::from(move |response: Result<()>| { let mut state = cloned_state.deref().clone(); state.submitted = false; - match state.view { - View::SignUp => match response { - Ok(_) => state.view = View::Verify, - Err(e) => state.status = e.to_string(), - }, - View::Verify => match response { - Ok(_) => callback.emit(()), - Err(e) => state.status = e.to_string(), - }, + match response { + Ok(_) => state.view = View::Verify, + Err(e) => state.status = e.to_string(), + } + + cloned_state.set(state); + }); + + let cloned_state = state.clone(); + let close_callback = props.close.clone(); + let sign_in_callback = callbacks.sign_in.clone(); + let on_verify_api_response = Callback::from(move |response: Result| { + let mut state = cloned_state.deref().clone(); + state.submitted = false; + + match response { + Ok(token) => { + sign_in_callback.emit(token); + close_callback.emit(()) + } + Err(e) => state.status = e.to_string(), } cloned_state.set(state); @@ -138,9 +151,9 @@ pub fn SignUp(props: &Props) -> Html { state.username.clone(), state.password.clone(), state.email.clone(), - on_api_response.clone(), + on_sign_up_api_response.clone(), ), - View::Verify => session.verify(state.token.clone(), on_api_response.clone()), + View::Verify => session.verify(state.token.clone(), on_verify_api_response.clone()), } cloned_state.set(state);