feat(api): fully implemented the verify endpoint

This commit is contained in:
antifallobst 2023-08-17 00:44:57 +02:00
parent b794574850
commit 1477e4cef6
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
2 changed files with 28 additions and 2 deletions

View File

@ -33,7 +33,10 @@ pub async fn register(
return Ok(data::RegisterResponse::Blocked);
}
if Account::from_username(pool, &request.username).await?.is_some() {
if Account::from_username(pool, &request.username)
.await?
.is_some()
{
return Ok(data::RegisterResponse::Conflict(
data::RegisterConflict::Username,
));
@ -59,7 +62,10 @@ pub async fn register(
SmtpClientBuilder::new(&std::env::var("SMTP_HOST_URL")?, 465)
.implicit_tls(true)
.credentials(mail_send::Credentials::Plain { username: &std::env::var("SMTP_USER")?, secret: &std::env::var("SMTP_PASSWORD")? })
.credentials(mail_send::Credentials::Plain {
username: &std::env::var("SMTP_USER")?,
secret: &std::env::var("SMTP_PASSWORD")?,
})
.connect()
.await?
.send(message)
@ -78,6 +84,13 @@ pub async fn verify(
return Ok(data::VerifyResponse::Blocked);
}
let token = match VerificationToken::check(pool, request.token).await? {
Some(t) => t,
None => return Ok(data::VerifyResponse::TokenUnknown),
};
token.apply(pool).await?;
Ok(data::VerifyResponse::Success)
}

View File

@ -1,3 +1,4 @@
use crate::accounts::Account;
use anyhow::{Error, Result};
use sqlx::{mysql::MySqlPool, types::chrono as sqlx_chrono};
@ -159,4 +160,16 @@ impl VerificationToken {
Err(e) => Err(Error::new(e)),
}
}
pub async fn apply(&self, pool: &MySqlPool) -> Result<()> {
sqlx::query!(r#"DELETE FROM VerificationTokens WHERE token = ?;"#, self.token)
.execute(pool)
.await?;
sqlx::query!(r#"UPDATE Accounts SET verified=true WHERE id = ?;"#, self.account)
.execute(pool)
.await?;
Ok(())
}
}