aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/error/client.rs8
-rw-r--r--src/routers/account.rs36
2 files changed, 20 insertions, 24 deletions
diff --git a/src/error/client.rs b/src/error/client.rs
index 70b6001..302581e 100644
--- a/src/error/client.rs
+++ b/src/error/client.rs
@@ -1,7 +1,7 @@
1pub enum ClientError { 1pub enum ClientError {
2 BadJsonBody(String), 2 BadJsonBody(String),
3 BadAuthTokenHeader(String), 3 BadAuthTokenHeader(String),
4 UserAlreadyExists { username: String }, 4 UsernameIsTaken { username: String },
5 InvalidPassword, 5 InvalidPassword,
6 NotAuthorized, 6 NotAuthorized,
7 UserNotFound { id: i64 }, 7 UserNotFound { id: i64 },
@@ -13,7 +13,7 @@ impl ClientError {
13 match self { 13 match self {
14 Self::BadJsonBody(..) => "BadJsonBody", 14 Self::BadJsonBody(..) => "BadJsonBody",
15 Self::BadAuthTokenHeader(..) => "BadAuthTokenHeader", 15 Self::BadAuthTokenHeader(..) => "BadAuthTokenHeader",
16 Self::UserAlreadyExists { .. } => "UserAlreadyExists", 16 Self::UsernameIsTaken { .. } => "UsernameIsTaken",
17 Self::InvalidPassword => "InvalidPassword", 17 Self::InvalidPassword => "InvalidPassword",
18 Self::NotAuthorized => "NotAuthorized", 18 Self::NotAuthorized => "NotAuthorized",
19 Self::UserNotFound { .. } => "UserNotFound", 19 Self::UserNotFound { .. } => "UserNotFound",
@@ -26,8 +26,8 @@ impl ClientError {
26 match self { 26 match self {
27 Self::BadJsonBody(msg) => msg, 27 Self::BadJsonBody(msg) => msg,
28 Self::BadAuthTokenHeader(msg) => msg, 28 Self::BadAuthTokenHeader(msg) => msg,
29 Self::UserAlreadyExists { username } => { 29 Self::UsernameIsTaken { username } => {
30 format!("user with username `{}` already exists", username) 30 format!("username `{}` is taken", username)
31 } 31 }
32 Self::InvalidPassword => format!("password is invalid"), 32 Self::InvalidPassword => format!("password is invalid"),
33 33
diff --git a/src/routers/account.rs b/src/routers/account.rs
index a6fce99..71ba496 100644
--- a/src/routers/account.rs
+++ b/src/routers/account.rs
@@ -2,22 +2,30 @@ use axum::extract::State;
2use chrono::{DateTime, Duration, Utc}; 2use chrono::{DateTime, Duration, Utc};
3use entity::users::{self}; 3use entity::users::{self};
4use sea_orm::{ 4use sea_orm::{
5 ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, IntoActiveModel, ModelTrait, 5 ActiveModelTrait, ActiveValue::Set, ColumnTrait, DatabaseConnection, EntityTrait,
6 QueryFilter, 6 IntoActiveModel, ModelTrait, QueryFilter,
7}; 7};
8use serde::{Deserialize, Serialize}; 8use serde::{Deserialize, Serialize};
9use utoipa::ToSchema; 9use utoipa::ToSchema;
10use utoipa_axum::{router::OpenApiRouter, routes}; 10use utoipa_axum::{router::OpenApiRouter, routes};
11 11
12use crate::{ 12use crate::{
13 ApiResult, AppState, ClientError, GlobalResponses, JwtClaims, ServerError, SuccessResponse, 13 ApiError, ApiResult, AppState, ClientError, GlobalResponses, JwtClaims, ServerError,
14 create_jwt, create_password, 14 SuccessResponse, create_jwt, create_password,
15 extract::{ApiJson, Auth}, 15 extract::{ApiJson, Auth},
16 models::Account, 16 models::Account,
17 tags::ACCOUNT, 17 tags::ACCOUNT,
18 validate_password, 18 validate_password,
19}; 19};
20 20
21async fn username_exists(username: &str, db: &DatabaseConnection) -> Result<bool, ApiError> {
22 Ok(users::Entity::find()
23 .filter(users::Column::Username.eq(username))
24 .one(db)
25 .await?
26 .is_some())
27}
28
21#[derive(Serialize, ToSchema)] 29#[derive(Serialize, ToSchema)]
22#[schema(description = "Authorization token information")] 30#[schema(description = "Authorization token information")]
23struct Token { 31struct Token {
@@ -130,14 +138,8 @@ async fn register(
130 State(state): State<AppState>, 138 State(state): State<AppState>,
131 ApiJson(req): ApiJson<RegisterRequest>, 139 ApiJson(req): ApiJson<RegisterRequest>,
132) -> ApiResult<Account> { 140) -> ApiResult<Account> {
133 let user_exists = users::Entity::find() 141 if username_exists(&req.username, &state.db).await? {
134 .filter(users::Column::Username.eq(&req.username)) 142 return Err(ClientError::UsernameIsTaken {
135 .one(&state.db)
136 .await?
137 .is_some();
138
139 if user_exists {
140 return Err(ClientError::UserAlreadyExists {
141 username: req.username, 143 username: req.username,
142 } 144 }
143 .into()); 145 .into());
@@ -255,14 +257,8 @@ async fn change_username(
255 Auth(user): Auth, 257 Auth(user): Auth,
256 ApiJson(req): ApiJson<ChangeUsernameRequest>, 258 ApiJson(req): ApiJson<ChangeUsernameRequest>,
257) -> ApiResult<Account> { 259) -> ApiResult<Account> {
258 let user_exists = users::Entity::find() 260 if username_exists(&req.new_username, &state.db).await? {
259 .filter(users::Column::Username.eq(&req.new_username)) 261 return Err(ClientError::UsernameIsTaken {
260 .one(&state.db)
261 .await?
262 .is_some();
263
264 if user_exists {
265 return Err(ClientError::UserAlreadyExists {
266 username: req.new_username, 262 username: req.new_username,
267 } 263 }
268 .into()); 264 .into());