Backend is pretty much done
This commit is contained in:
parent
eebb8594de
commit
c8a6acd07d
4 changed files with 43 additions and 7 deletions
|
@ -2,7 +2,7 @@ use crate::{
|
||||||
db_connection::DbPool,
|
db_connection::DbPool,
|
||||||
errors::CustomError,
|
errors::CustomError,
|
||||||
handlers::pool_handler,
|
handlers::pool_handler,
|
||||||
models::user::{AuthUser, RegisterUser, User},
|
models::user::{AuthUser, DeleteUser, RegisterUser, User},
|
||||||
utils::jwt::{decode_token, encode_token, UserWithToken},
|
utils::jwt::{decode_token, encode_token, UserWithToken},
|
||||||
};
|
};
|
||||||
use actix_identity::Identity;
|
use actix_identity::Identity;
|
||||||
|
@ -42,10 +42,30 @@ pub async fn register(
|
||||||
.map_err(|e| HttpResponse::InternalServerError().json(e.to_string()))?;
|
.map_err(|e| HttpResponse::InternalServerError().json(e.to_string()))?;
|
||||||
|
|
||||||
User::create(register_user, &connection)
|
User::create(register_user, &connection)
|
||||||
.map(|user| HttpResponse::Ok().json(user))
|
.map(|_r| HttpResponse::Ok().json("User created successfully"))
|
||||||
.map_err(|e| HttpResponse::InternalServerError().json(e.to_string()))
|
.map_err(|e| HttpResponse::InternalServerError().json(e.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[post("/auth/delete")]
|
||||||
|
pub async fn delete(
|
||||||
|
user: LoggedUser,
|
||||||
|
user_to_delete: web::Json<DeleteUser>,
|
||||||
|
pool: web::Data<DbPool>,
|
||||||
|
) -> Result<HttpResponse, HttpResponse> {
|
||||||
|
if user.admin || user.username == user_to_delete.username {
|
||||||
|
let connection = pool_handler(pool)?;
|
||||||
|
user_to_delete.delete(&connection).map_err(|e| match e {
|
||||||
|
CustomError::DBError(diesel::result::Error::NotFound) => {
|
||||||
|
HttpResponse::NotFound().json(e.to_string())
|
||||||
|
}
|
||||||
|
_ => HttpResponse::InternalServerError().json(e.to_string()),
|
||||||
|
})?;
|
||||||
|
Ok(HttpResponse::Ok().json("User deleted successfully"))
|
||||||
|
} else {
|
||||||
|
Err(HttpResponse::Unauthorized().json("Only admins can delete users"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[post("/auth/login")]
|
#[post("/auth/login")]
|
||||||
pub async fn login(
|
pub async fn login(
|
||||||
id: Identity,
|
id: Identity,
|
||||||
|
@ -65,8 +85,8 @@ pub async fn login(
|
||||||
_ => HttpResponse::InternalServerError().finish(),
|
_ => HttpResponse::InternalServerError().finish(),
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
id.remember(String::from(&token));
|
id.remember(String::from(token));
|
||||||
Ok(HttpResponse::Ok().json(token))
|
Ok(HttpResponse::Ok().json(user))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[delete("/auth/logout")]
|
#[delete("/auth/logout")]
|
||||||
|
|
|
@ -70,6 +70,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
.service(authentication::register)
|
.service(authentication::register)
|
||||||
.service(authentication::login)
|
.service(authentication::login)
|
||||||
.service(authentication::logout)
|
.service(authentication::logout)
|
||||||
|
.service(authentication::delete)
|
||||||
.service(handlers::hello_world::hello)
|
.service(handlers::hello_world::hello)
|
||||||
})
|
})
|
||||||
.bind(address)?
|
.bind(address)?
|
||||||
|
|
|
@ -35,7 +35,7 @@ impl User {
|
||||||
pub fn create(
|
pub fn create(
|
||||||
register_user: RegisterUser,
|
register_user: RegisterUser,
|
||||||
connection: &MysqlConnection,
|
connection: &MysqlConnection,
|
||||||
) -> Result<bool, CustomError> {
|
) -> Result<usize, CustomError> {
|
||||||
use crate::schema::users::dsl::users;
|
use crate::schema::users::dsl::users;
|
||||||
|
|
||||||
let new_user = NewUser {
|
let new_user = NewUser {
|
||||||
|
@ -47,8 +47,7 @@ impl User {
|
||||||
|
|
||||||
Ok(diesel::insert_into(users)
|
Ok(diesel::insert_into(users)
|
||||||
.values(new_user)
|
.values(new_user)
|
||||||
.execute(connection)
|
.execute(connection)?)
|
||||||
.is_ok())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,3 +102,18 @@ impl AuthUser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct DeleteUser {
|
||||||
|
pub username: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeleteUser {
|
||||||
|
pub fn delete(&self, connection: &MysqlConnection) -> Result<bool, CustomError> {
|
||||||
|
use crate::schema::users::dsl::*;
|
||||||
|
match diesel::delete(users.filter(username.eq(&self.username))).execute(connection) {
|
||||||
|
Ok(_r) => Ok(true),
|
||||||
|
Err(e) => Err(CustomError::DBError(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ struct Claims {
|
||||||
exp: usize,
|
exp: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
pub struct UserWithToken {
|
pub struct UserWithToken {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue