feat: gacha roll, user auth
This commit is contained in:
@@ -29,8 +29,15 @@ pub struct AuthRegisterRequestDto {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct AuthQueryByEmailResponse {
|
||||
pub struct AuthActiveInactiveRequestDto {
|
||||
pub is_active: bool,
|
||||
pub email: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct AuthQueryByEmailResponseDto {
|
||||
pub email: String,
|
||||
pub fullname: String,
|
||||
pub password: String,
|
||||
pub is_active: bool,
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
use crate::{common_response, extract_email, AppState};
|
||||
use super::AuthRepository;
|
||||
use crate::{
|
||||
common_response, extract_email, v1::users_schema::UsersSchema, AppState,
|
||||
};
|
||||
use axum::{
|
||||
extract::Request, http::StatusCode, middleware::Next, response::Response,
|
||||
Extension,
|
||||
};
|
||||
use std::convert::Infallible;
|
||||
|
||||
use super::{AuthQueryByEmailResponse, AuthRepository};
|
||||
|
||||
pub async fn auth_middleware(
|
||||
Extension(state): Extension<AppState>,
|
||||
mut req: Request,
|
||||
@@ -26,16 +27,15 @@ pub async fn auth_middleware(
|
||||
|
||||
let repository = AuthRepository::new(&state);
|
||||
|
||||
let user: Option<AuthQueryByEmailResponse> =
|
||||
match repository.query_user_by_email(email).await {
|
||||
Ok(user) => Some(user),
|
||||
Err(err) => {
|
||||
return Ok(common_response(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
&format!("DB error: {}", err),
|
||||
))
|
||||
}
|
||||
};
|
||||
let user: Option<UsersSchema> = match repository.query_user_by_email(email).await {
|
||||
Ok(user) => Some(user),
|
||||
Err(err) => {
|
||||
return Ok(common_response(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
&err.to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
if user.is_none() {
|
||||
return Ok(common_response(
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use crate::{v1::UsersItemDto, AppState, RedisKeyEnum, ResourceEnum};
|
||||
use super::{AuthActiveInactiveRequestDto, AuthRegisterRequestDto};
|
||||
use crate::{
|
||||
v1::{users_schema::UsersSchema, UsersItemDto},
|
||||
AppState, RedisKeyEnum, ResourceEnum,
|
||||
};
|
||||
use anyhow::{bail, Result};
|
||||
use redis::Commands;
|
||||
|
||||
use super::{AuthQueryByEmailResponse, AuthRegisterRequestDto};
|
||||
|
||||
pub struct AuthRepository<'a> {
|
||||
state: &'a AppState,
|
||||
}
|
||||
@@ -33,26 +35,19 @@ impl<'a> AuthRepository<'a> {
|
||||
pub fn query_get_stored_user(&self, email: String) -> Result<UsersItemDto> {
|
||||
let redis_key = format!("{}:{}", RedisKeyEnum::User, email);
|
||||
let mut conn = self.state.redisdb.get_connection()?;
|
||||
|
||||
let data: Option<String> = conn.get(&redis_key)?;
|
||||
|
||||
match data {
|
||||
Some(user_json) => {
|
||||
let user: UsersItemDto = serde_json::from_str(&user_json)?;
|
||||
Ok(user)
|
||||
}
|
||||
None => bail!("No stored user data found for email"),
|
||||
None => bail!("No stored user data found"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn query_user_by_email(
|
||||
&self,
|
||||
email: String,
|
||||
) -> Result<AuthQueryByEmailResponse> {
|
||||
pub async fn query_user_by_email(&self, email: String) -> Result<UsersSchema> {
|
||||
let db = &self.state.surrealdb;
|
||||
|
||||
let result = db.select((ResourceEnum::Users.to_string(), email)).await?;
|
||||
|
||||
match result {
|
||||
Some(response) => Ok(response),
|
||||
None => bail!("User not found"),
|
||||
@@ -64,15 +59,28 @@ impl<'a> AuthRepository<'a> {
|
||||
data: AuthRegisterRequestDto,
|
||||
) -> Result<String> {
|
||||
let db = &self.state.surrealdb;
|
||||
|
||||
let record: Option<UsersItemDto> = db
|
||||
.create((ResourceEnum::Users.to_string(), &data.email))
|
||||
.content(data)
|
||||
.await?;
|
||||
|
||||
match record {
|
||||
Some(_) => Ok("Success create user".into()),
|
||||
None => bail!("Failed to create user"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn query_active_inactive_user(
|
||||
&self,
|
||||
data: AuthActiveInactiveRequestDto,
|
||||
) -> Result<String> {
|
||||
let db = &self.state.surrealdb;
|
||||
let record: Option<UsersItemDto> = db
|
||||
.update((ResourceEnum::Users.to_string(), &data.email))
|
||||
.content(data)
|
||||
.await?;
|
||||
match record {
|
||||
Some(_) => Ok("Success update user".into()),
|
||||
None => bail!("Failed to update user"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,7 @@ use super::{
|
||||
};
|
||||
use crate::{
|
||||
common_response, encode_access_token, encode_refresh_token, hash_password,
|
||||
success_response, v1::UsersItemDto, verify_password, AppState,
|
||||
ResponseSuccessDto,
|
||||
success_response, v1::UsersItemDto, verify_password, AppState, ResponseSuccessDto,
|
||||
};
|
||||
|
||||
pub struct AuthService;
|
||||
@@ -21,14 +20,10 @@ impl AuthService {
|
||||
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);
|
||||
verify_password(&payload.password, &user.password).unwrap_or(false);
|
||||
|
||||
if is_password_correct {
|
||||
common_response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Email or password not correct",
|
||||
);
|
||||
common_response(StatusCode::BAD_REQUEST, "Email or password not correct");
|
||||
}
|
||||
|
||||
let access_token = encode_access_token(payload.email.clone());
|
||||
@@ -39,6 +34,7 @@ impl AuthService {
|
||||
user: UsersItemDto {
|
||||
fullname: user.fullname.clone(),
|
||||
email: user.email.clone(),
|
||||
is_active: user.is_active.clone(),
|
||||
},
|
||||
token: TokenDto {
|
||||
access_token: access_token.unwrap(),
|
||||
@@ -55,10 +51,7 @@ impl AuthService {
|
||||
})
|
||||
.is_ok()
|
||||
{
|
||||
return common_response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Failed to store data",
|
||||
);
|
||||
return common_response(StatusCode::BAD_REQUEST, "Failed to store data");
|
||||
}
|
||||
|
||||
success_response(response)
|
||||
|
||||
Reference in New Issue
Block a user