1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use actix_web::{HttpResponse, ResponseError};
use derive_more::Display;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserSignInBody {
    pub login: String,
    pub password: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserSignInResult {
    pub id: String,
    pub token: String,
    pub kind: String,
    pub activation_status: String,
}

#[derive(Debug, Display)]
pub enum UserSignInError {
    #[display(fmt = "error_hashing_password")]
    HashPassword,
    #[display(fmt = "invalid_password")]
    InvalidPassword,
    #[display(fmt = "user_not_found")]
    UserNotFound,
    #[display(fmt = "user_not_verified")]
    UserNotVerified,
    #[display(fmt = "inactif_user")]
    InActifUser,
    Default(String),
}

impl ResponseError for UserSignInError {
    fn error_response(&self) -> HttpResponse {
        match self {
            UserSignInError::HashPassword => {
                HttpResponse::InternalServerError().json("error_hashing_password")
            }
            UserSignInError::InvalidPassword => {
                HttpResponse::Unauthorized().json("invalid_password")
            }
            UserSignInError::UserNotFound => HttpResponse::NotFound().json("user_not_found"),
            UserSignInError::UserNotVerified => {
                HttpResponse::NotAcceptable().json("user_not_verified")
            }
            UserSignInError::InActifUser => HttpResponse::Conflict().json("inactif_user"),
            UserSignInError::Default(error) => HttpResponse::BadRequest().body(error),
        }
    }
}