forked from Roflin/gamenight
Added Login and Register handlers for actix backend
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use std::fmt::{Display, Formatter, Result};
|
||||
use actix_web::{ResponseError, error::BlockingError};
|
||||
use actix_web::{ResponseError, error::BlockingError, HttpResponse, http::{header::ContentType, StatusCode}};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use validator::ValidationErrors;
|
||||
|
||||
@@ -7,22 +7,32 @@ use crate::schema::error::DatabaseError;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct ApiError {
|
||||
pub error: String
|
||||
#[serde(skip_serializing)]
|
||||
pub status: u16,
|
||||
pub message: String
|
||||
}
|
||||
|
||||
impl Display for ApiError {
|
||||
// This trait requires `fmt` with this exact signature.
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
write!(f, "{}", self.error)
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseError for ApiError { }
|
||||
impl ResponseError for ApiError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
HttpResponse::build(StatusCode::from_u16(self.status).unwrap())
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&self).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DatabaseError> for ApiError {
|
||||
fn from(value: DatabaseError) -> Self {
|
||||
ApiError {
|
||||
error: value.0
|
||||
//Todo, split this in unrecoverable and schema error
|
||||
status: 500,
|
||||
message: value.0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +40,8 @@ impl From<DatabaseError> for ApiError {
|
||||
impl From<BlockingError> for ApiError {
|
||||
fn from(value: BlockingError) -> Self {
|
||||
ApiError {
|
||||
error: value.to_string()
|
||||
status: 500,
|
||||
message: value.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,15 +49,17 @@ impl From<BlockingError> for ApiError {
|
||||
impl From<serde_json::Error> for ApiError {
|
||||
fn from(value: serde_json::Error) -> Self {
|
||||
ApiError {
|
||||
error: value.to_string()
|
||||
status: 500,
|
||||
message: value.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<jsonwebtoken::errors::Error> for ApiError {
|
||||
fn from(value: jsonwebtoken::errors::Error) -> Self {
|
||||
ApiError {
|
||||
error: value.to_string()
|
||||
ApiError {
|
||||
status: 500,
|
||||
message: value.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,7 +67,8 @@ impl From<jsonwebtoken::errors::Error> for ApiError {
|
||||
impl From<ValidationErrors> for ApiError {
|
||||
fn from(value: ValidationErrors) -> Self {
|
||||
ApiError {
|
||||
error: value.to_string()
|
||||
status: 422,
|
||||
message: value.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use serde::{Serialize, Deserialize};
|
||||
use uuid::Uuid;
|
||||
use validator::ValidateArgs;
|
||||
use crate::DbPool;
|
||||
use crate::request::request_data::{Login, Register};
|
||||
use crate::request::requests::{Login, Register};
|
||||
use crate::request::error::ApiError;
|
||||
use crate::request::responses::LoginResponse;
|
||||
use crate::schema::user::Role;
|
||||
@@ -83,6 +83,7 @@ pub async fn register(pool: web::Data<DbPool>, register_data: web::Json<Register
|
||||
let mut conn1 = pool.get().expect("couldn't get db connection from pool");
|
||||
let mut conn2 = pool.get().expect("couldn't get db connection from pool");
|
||||
|
||||
|
||||
let _validation_result = web::block(move || {
|
||||
data1.validate_args((&mut conn1, &mut conn2))
|
||||
}).await??;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
mod request_data;
|
||||
mod requests;
|
||||
mod responses;
|
||||
mod handler;
|
||||
mod error;
|
||||
|
||||
Reference in New Issue
Block a user