summaryrefslogtreecommitdiffstats
path: root/src/routes/auth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/auth.rs')
-rw-r--r--src/routes/auth.rs68
1 files changed, 0 insertions, 68 deletions
diff --git a/src/routes/auth.rs b/src/routes/auth.rs
deleted file mode 100644
index 0c459f5..0000000
--- a/src/routes/auth.rs
+++ /dev/null
@@ -1,68 +0,0 @@
-use crate::{
- errors::AppError,
- models::{
- auth::{AuthBody, Claims, LoginCredentials, SignUpForm},
- user::User,
- },
- routes::JsonCreate,
-};
-use axum::{routing::post, Json, Router};
-
-/// Create routes for `/v1/auth/` namespace
-pub fn create_route() -> Router {
- Router::new()
- .route("/login", post(make_login))
- .route("/signup", post(signup))
-}
-
-/// Make login. Check if a user with the email and password passed in request body exists into the
-/// database
-async fn make_login(Json(payload): Json<LoginCredentials>) -> Result<Json<AuthBody>, AppError> {
- let user = User::new(
- String::new(),
- String::new(),
- payload.username,
- payload.password,
- );
- match User::find(user).await {
- Ok(user) => {
- let claims = Claims::new(user.id);
- let token = claims.get_token()?;
- Ok(Json(AuthBody::new(token)))
- }
- Err(_) => Err(AppError::NotFound("User not found".to_string())),
- }
-}
-
-/// Create a new user
-async fn signup(Json(payload): Json<SignUpForm>) -> Result<JsonCreate<AuthBody>, AppError> {
- if payload.password1 != payload.password2 {
- return Err(AppError::BadRequest(
- "The inserted passwords do not match".to_string(),
- ));
- }
-
- if User::email_has_taken(&payload.email).await? {
- return Err(AppError::BadRequest(
- "An user with this email already exists".to_string(),
- ));
- }
-
- if User::username_has_taken(&payload.username).await? {
- return Err(AppError::BadRequest(
- "An user with this username already exists".to_string(),
- ));
- }
-
- let user = User::new(
- payload.name,
- payload.email,
- payload.username,
- payload.password1,
- );
- let user = User::create(user).await?;
-
- let claims = Claims::new(user.id);
- let token = claims.get_token()?;
- Ok(JsonCreate(AuthBody::new(token)))
-}