2025-03-13 23:40:16 +07:00
|
|
|
use axum::{http::StatusCode, response::Response};
|
|
|
|
|
|
|
|
|
|
use super::{
|
2025-03-14 09:53:20 +07:00
|
|
|
AuthLoginRequestDto, AuthLoginResponsetDto, AuthRegisterRequestDto,
|
|
|
|
|
AuthRepository, TokenDto,
|
|
|
|
|
};
|
|
|
|
|
use crate::{
|
|
|
|
|
common_response, encode_access_token, encode_refresh_token, hash_password,
|
|
|
|
|
success_response, v1::UsersItemDto, verify_password, AppState,
|
2025-03-15 01:53:20 +07:00
|
|
|
ResponseSuccessDto,
|
2025-03-13 23:40:16 +07:00
|
|
|
};
|
|
|
|
|
|
2025-03-14 09:53:20 +07:00
|
|
|
pub struct AuthService;
|
2025-03-13 23:40:16 +07:00
|
|
|
|
2025-03-14 09:53:20 +07:00
|
|
|
impl AuthService {
|
|
|
|
|
pub async fn mutation_login(
|
|
|
|
|
payload: AuthLoginRequestDto,
|
|
|
|
|
state: &AppState,
|
|
|
|
|
) -> Response {
|
|
|
|
|
let repository = AuthRepository::new(state);
|
|
|
|
|
match repository.query_user_by_email(payload.email.clone()).await {
|
|
|
|
|
Ok(user) => {
|
|
|
|
|
let is_password_correct =
|
|
|
|
|
verify_password(&payload.password, &user.password)
|
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
|
|
|
|
|
if is_password_correct {
|
|
|
|
|
common_response(
|
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
|
"Email or password not correct",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-15 01:53:20 +07:00
|
|
|
let access_token = encode_access_token(payload.email.clone());
|
|
|
|
|
let refresh_token = encode_refresh_token(payload.email.clone());
|
2025-03-14 09:53:20 +07:00
|
|
|
|
|
|
|
|
let response = ResponseSuccessDto {
|
|
|
|
|
data: AuthLoginResponsetDto {
|
|
|
|
|
user: UsersItemDto {
|
|
|
|
|
fullname: user.fullname.clone(),
|
|
|
|
|
email: user.email.clone(),
|
|
|
|
|
},
|
|
|
|
|
token: TokenDto {
|
|
|
|
|
access_token: access_token.unwrap(),
|
|
|
|
|
refresh_token: refresh_token.unwrap(),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-14 17:50:37 +07:00
|
|
|
if !repository
|
|
|
|
|
.query_store_user_data(AuthRegisterRequestDto {
|
|
|
|
|
fullname: user.fullname,
|
|
|
|
|
password: user.password,
|
|
|
|
|
email: user.email,
|
|
|
|
|
})
|
|
|
|
|
.is_ok()
|
|
|
|
|
{
|
|
|
|
|
return common_response(
|
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
|
"Failed to store data",
|
|
|
|
|
);
|
2025-03-14 09:53:20 +07:00
|
|
|
}
|
2025-03-14 17:50:37 +07:00
|
|
|
|
|
|
|
|
success_response(response)
|
2025-03-14 09:53:20 +07:00
|
|
|
}
|
|
|
|
|
Err(err) => common_response(StatusCode::UNAUTHORIZED, &err.to_string()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn mutation_register(
|
|
|
|
|
payload: AuthRegisterRequestDto,
|
|
|
|
|
state: &AppState,
|
|
|
|
|
) -> Response {
|
|
|
|
|
let repository = AuthRepository::new(state);
|
|
|
|
|
if repository
|
|
|
|
|
.query_user_by_email(payload.email.clone())
|
|
|
|
|
.await
|
|
|
|
|
.is_ok()
|
|
|
|
|
{
|
|
|
|
|
return common_response(StatusCode::BAD_REQUEST, "User already exists");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let hashed_password = match hash_password(&payload.password) {
|
|
|
|
|
Ok(hash) => hash,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
return common_response(
|
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
|
"Failed to hash password",
|
2025-03-13 23:40:16 +07:00
|
|
|
);
|
|
|
|
|
}
|
2025-03-14 09:53:20 +07:00
|
|
|
};
|
2025-03-13 23:40:16 +07:00
|
|
|
|
2025-03-14 09:53:20 +07:00
|
|
|
let new_user = AuthRegisterRequestDto {
|
|
|
|
|
email: payload.email,
|
|
|
|
|
password: hashed_password,
|
|
|
|
|
fullname: payload.fullname,
|
|
|
|
|
};
|
2025-03-13 23:40:16 +07:00
|
|
|
|
2025-03-14 09:53:20 +07:00
|
|
|
match repository.query_create_user(new_user).await {
|
|
|
|
|
Ok(_) => common_response(StatusCode::CREATED, "Registration successful"),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
common_response(StatusCode::INTERNAL_SERVER_ERROR, &err.to_string())
|
|
|
|
|
}
|
2025-03-13 23:40:16 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|