feat(api): fully implemented the delete endpoint

This commit is contained in:
antifallobst 2023-08-17 02:17:11 +02:00
parent 3049b68ef4
commit 5f5534f597
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
1 changed files with 14 additions and 1 deletions

View File

@ -33,7 +33,9 @@ pub async fn register(
return Ok(data::RegisterResponse::Blocked);
}
let email_regex = regex::Regex::new(r"^([a-z0-9_+]([a-z0-9_+.]*[a-z0-9_+])?)@([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6})")?;
let email_regex = regex::Regex::new(
r"^([a-z0-9_+]([a-z0-9_+.]*[a-z0-9_+])?)@([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6})",
)?;
if !email_regex.is_match(&request.email) {
return Ok(data::RegisterResponse::MalformedEmail);
@ -133,7 +135,18 @@ pub async fn delete(pool: &MySqlPool, token: String) -> Result<data::DeleteRespo
return Ok(data::DeleteResponse::Blocked);
}
let token = match AuthToken::check(pool, token).await? {
Some(t) => t,
None => return Ok(data::DeleteResponse::Unauthorized),
};
sqlx::query!(r#"DELETE FROM AuthTokens WHERE account = ?;"#, token.account)
.execute(pool)
.await?;
sqlx::query!(r#"DELETE FROM Accounts WHERE id = ?;"#, token.account)
.execute(pool)
.await?;
Ok(data::DeleteResponse::Success)
}