style: cargo clippy && cargo fmt
This commit is contained in:
parent
c9a3e8c6c4
commit
d615f1392e
31 changed files with 288 additions and 181 deletions
|
@ -4,9 +4,7 @@ use actix_web::{HttpResponse, get, post, web};
|
|||
use chrono::{Duration, Utc};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
error::Error, structs::PasswordResetToken, Data
|
||||
};
|
||||
use crate::{Data, error::Error, structs::PasswordResetToken};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Query {
|
||||
|
@ -14,30 +12,31 @@ struct Query {
|
|||
}
|
||||
|
||||
/// `GET /api/v1/auth/reset-password` Sends password reset email to user
|
||||
///
|
||||
///
|
||||
/// requires auth? no
|
||||
///
|
||||
///
|
||||
/// ### Query Parameters
|
||||
/// identifier: Email or username
|
||||
///
|
||||
///
|
||||
/// ### Responses
|
||||
/// 200 Email sent
|
||||
/// 429 Too Many Requests
|
||||
/// 404 Not found
|
||||
/// 400 Bad request
|
||||
///
|
||||
///
|
||||
#[get("/reset-password")]
|
||||
pub async fn get(
|
||||
query: web::Query<Query>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
pub async fn get(query: web::Query<Query>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
let mut conn = data.pool.get().await?;
|
||||
|
||||
if let Ok(password_reset_token) = PasswordResetToken::get_with_identifier(&mut conn, query.identifier.clone()).await {
|
||||
if let Ok(password_reset_token) =
|
||||
PasswordResetToken::get_with_identifier(&mut conn, query.identifier.clone()).await
|
||||
{
|
||||
if Utc::now().signed_duration_since(password_reset_token.created_at) > Duration::hours(1) {
|
||||
password_reset_token.delete(&mut conn).await?;
|
||||
} else {
|
||||
return Err(Error::TooManyRequests("Please allow 1 hour before sending a new email".to_string()))
|
||||
return Err(Error::TooManyRequests(
|
||||
"Please allow 1 hour before sending a new email".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,9 +52,9 @@ struct ResetPassword {
|
|||
}
|
||||
|
||||
/// `POST /api/v1/auth/reset-password` Resets user password
|
||||
///
|
||||
///
|
||||
/// requires auth? no
|
||||
///
|
||||
///
|
||||
/// ### Request Example:
|
||||
/// ```
|
||||
/// json!({
|
||||
|
@ -63,13 +62,13 @@ struct ResetPassword {
|
|||
/// "token": "a3f7e29c1b8d0456e2c9f83b7a1d6e4f5028c3b9a7e1f2d5c6b8a0d3e7f4a2b"
|
||||
/// });
|
||||
/// ```
|
||||
///
|
||||
///
|
||||
/// ### Responses
|
||||
/// 200 Success
|
||||
/// 410 Token Expired
|
||||
/// 404 Not Found
|
||||
/// 400 Bad Request
|
||||
///
|
||||
///
|
||||
#[post("/reset-password")]
|
||||
pub async fn post(
|
||||
reset_password: web::Json<ResetPassword>,
|
||||
|
@ -77,14 +76,17 @@ pub async fn post(
|
|||
) -> Result<HttpResponse, Error> {
|
||||
let mut conn = data.pool.get().await?;
|
||||
|
||||
let password_reset_token = PasswordResetToken::get(&mut conn, reset_password.token.clone()).await?;
|
||||
let password_reset_token =
|
||||
PasswordResetToken::get(&mut conn, reset_password.token.clone()).await?;
|
||||
|
||||
if Utc::now().signed_duration_since(password_reset_token.created_at) > Duration::hours(24) {
|
||||
password_reset_token.delete(&mut conn).await?;
|
||||
return Ok(HttpResponse::Gone().finish());
|
||||
}
|
||||
|
||||
password_reset_token.set_password(&data, reset_password.password.clone()).await?;
|
||||
password_reset_token
|
||||
.set_password(&data, reset_password.password.clone())
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue