feat: implemented auto login after account verification
This commit is contained in:
parent
d39277e182
commit
815d5face5
|
@ -32,3 +32,8 @@ pub enum AccountRegisterCriteriaProblemResponse {
|
|||
Email,
|
||||
Password,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct AccountVerifyResponse {
|
||||
pub token: String,
|
||||
}
|
||||
|
|
|
@ -64,16 +64,10 @@ impl Session {
|
|||
let response = request.send().await?;
|
||||
|
||||
match response.status() {
|
||||
200 => {
|
||||
let token = response
|
||||
200 => Ok(response
|
||||
.json::<data::AccountAuthenticateResponse>()
|
||||
.await?
|
||||
.token;
|
||||
|
||||
set_auth_cookie(&token);
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
.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<Result<()>>) {
|
||||
pub fn verify(&self, token: String, callback: Callback<Result<String>>) {
|
||||
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::<data::AccountVerifyResponse>().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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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::<Callbacks>().expect("Callbacks context not available!");
|
||||
let session = use_context::<Session>().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 {
|
||||
match response {
|
||||
Ok(_) => state.view = View::Verify,
|
||||
Err(e) => state.status = e.to_string(),
|
||||
},
|
||||
View::Verify => match response {
|
||||
Ok(_) => callback.emit(()),
|
||||
}
|
||||
|
||||
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<String>| {
|
||||
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);
|
||||
|
|
Loading…
Reference in New Issue