2025-03-13 23:40:16 +07:00
|
|
|
use axum::{http::StatusCode, response::Response};
|
2025-03-14 09:53:20 +07:00
|
|
|
use redis::Commands;
|
2025-03-13 23:40:16 +07:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
ResponseSuccessDto, TokenSub,
|
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",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let access_token = encode_access_token(TokenSub {
|
|
|
|
|
email: payload.email.clone(),
|
|
|
|
|
role_name: "Admin".to_string(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let refresh_token = encode_refresh_token(TokenSub {
|
|
|
|
|
email: payload.email.clone(),
|
|
|
|
|
role_name: "Admin".to_string(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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(),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let redis_key =
|
|
|
|
|
format!("authenticated_users_data:{}", payload.email.clone());
|
|
|
|
|
|
|
|
|
|
match state.redisdb.get_connection().and_then(|mut conn| {
|
|
|
|
|
conn.set_ex::<_, String, ()>(
|
|
|
|
|
&redis_key,
|
|
|
|
|
serde_json::to_string(&user).unwrap_or_default(),
|
|
|
|
|
86400,
|
|
|
|
|
)
|
|
|
|
|
}) {
|
|
|
|
|
Ok(_) => success_response(response),
|
|
|
|
|
Err(err) => common_response(
|
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
|
&format!("Redis storage failed: {}", err),
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|