feat: added support for account verification
This commit is contained in:
parent
f85081e449
commit
44095c4235
20
src/error.rs
20
src/error.rs
|
@ -18,7 +18,7 @@ pub enum AuthError {
|
|||
WrongPassword,
|
||||
|
||||
#[error("Failed to authenticate: account not found")]
|
||||
NotFound,
|
||||
UsernameUnknown,
|
||||
|
||||
#[error("Failed to authenticate: account not verified")]
|
||||
NotVerified,
|
||||
|
@ -53,3 +53,21 @@ pub enum RegisterError {
|
|||
#[error("Failed to register: password does not meet the criteria")]
|
||||
PasswordInvalid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum VerifyAccountError {
|
||||
#[error("Failed to verify account: the API complains about a malformed request")]
|
||||
BadRequest,
|
||||
|
||||
#[error("Failed to verify account: unprocessable API response")]
|
||||
BadResponse,
|
||||
|
||||
#[error("Failed to verify account: failed to send request")]
|
||||
RequestSend(reqwest::Error),
|
||||
|
||||
#[error("Failed to verify account: blocked for security reasons")]
|
||||
Forbidden,
|
||||
|
||||
#[error("Failed to verify account: unknown verification token")]
|
||||
UnknownToken,
|
||||
}
|
||||
|
|
35
src/lib.rs
35
src/lib.rs
|
@ -1,8 +1,8 @@
|
|||
mod error;
|
||||
pub mod error;
|
||||
pub mod request;
|
||||
pub mod response;
|
||||
|
||||
use crate::error::{AuthError, RegisterError};
|
||||
use crate::error::{AuthError, RegisterError, VerifyAccountError};
|
||||
use anyhow::Result;
|
||||
use reqwest::StatusCode;
|
||||
use std::fmt::Debug;
|
||||
|
@ -62,7 +62,7 @@ impl Session {
|
|||
StatusCode::BAD_REQUEST => Err(AuthError::BadRequest),
|
||||
StatusCode::UNAUTHORIZED => Err(AuthError::WrongPassword),
|
||||
StatusCode::FORBIDDEN => Err(AuthError::Forbidden),
|
||||
StatusCode::NOT_FOUND => Err(AuthError::NotFound),
|
||||
StatusCode::NOT_FOUND => Err(AuthError::UsernameUnknown),
|
||||
StatusCode::FAILED_DEPENDENCY => Err(AuthError::NotVerified),
|
||||
_ => Err(AuthError::BadResponse),
|
||||
}
|
||||
|
@ -117,6 +117,35 @@ impl Session {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn verify_account(&mut self, token: String) -> Result<(), VerifyAccountError> {
|
||||
let url = format!("{}/account/verify", &self.base_url);
|
||||
|
||||
let body = request::AccountVerify { token };
|
||||
|
||||
let request = self.client.post(&url).json(&body);
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| VerifyAccountError::RequestSend(e))?;
|
||||
|
||||
match response.status() {
|
||||
StatusCode::OK => {
|
||||
self.auth_token = Some(
|
||||
response
|
||||
.json::<response::AccountVerify>()
|
||||
.await
|
||||
.map_err(|_| VerifyAccountError::BadResponse)?
|
||||
.token,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
StatusCode::BAD_REQUEST => Err(VerifyAccountError::BadRequest),
|
||||
StatusCode::FORBIDDEN => Err(VerifyAccountError::Forbidden),
|
||||
StatusCode::NOT_FOUND => Err(VerifyAccountError::UnknownToken),
|
||||
_ => Err(VerifyAccountError::BadResponse),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_authenticated(&self) -> bool {
|
||||
self.auth_token.is_some()
|
||||
}
|
||||
|
|
|
@ -12,3 +12,8 @@ pub struct AccountRegister {
|
|||
pub password: String,
|
||||
pub email: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct AccountVerify {
|
||||
pub token: String,
|
||||
}
|
||||
|
|
|
@ -19,3 +19,8 @@ pub enum AccountRegisterUnprocessable {
|
|||
Email,
|
||||
Password,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct AccountVerify {
|
||||
pub token: String,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue