feat: basic auth
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
use super::{
|
||||
mutation_login, mutation_register, AuthLoginRequestDto, AuthRegisterRequestDto,
|
||||
};
|
||||
use super::{AuthLoginRequestDto, AuthRegisterRequestDto, AuthService};
|
||||
use crate::AppState;
|
||||
use axum::{response::IntoResponse, Extension, Json};
|
||||
|
||||
@@ -8,12 +6,12 @@ pub async fn post_login(
|
||||
Extension(state): Extension<AppState>,
|
||||
Json(payload): Json<AuthLoginRequestDto>,
|
||||
) -> impl IntoResponse {
|
||||
mutation_login(payload, &state).await
|
||||
AuthService::mutation_login(payload, &state).await
|
||||
}
|
||||
|
||||
pub async fn post_register(
|
||||
Extension(state): Extension<AppState>,
|
||||
Json(payload): Json<AuthRegisterRequestDto>,
|
||||
) -> impl IntoResponse {
|
||||
mutation_register(payload, &state).await
|
||||
AuthService::mutation_register(payload, &state).await
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
use crate::v1::UsersItemDto;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct AuthLoginRequestDto {
|
||||
pub email: String,
|
||||
@@ -10,6 +12,7 @@ pub struct AuthLoginRequestDto {
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct AuthLoginResponsetDto {
|
||||
pub token: TokenDto,
|
||||
pub user: UsersItemDto,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
|
||||
@@ -1,32 +1,45 @@
|
||||
use crate::{v1::UsersItemDto, AppState};
|
||||
use crate::{v1::UsersItemDto, AppState, ResourceEnum};
|
||||
use std::error::Error;
|
||||
|
||||
use super::AuthRegisterRequestDto;
|
||||
|
||||
pub async fn query_user_by_email(
|
||||
email: String,
|
||||
state: &AppState,
|
||||
) -> Result<UsersItemDto, Box<dyn Error>> {
|
||||
let db = &state.surrealdb;
|
||||
|
||||
let mut result = db
|
||||
.query("SELECT * FROM app_users WHERE email = $email LIMIT 1;")
|
||||
.bind(("email", email.clone()))
|
||||
.await?;
|
||||
|
||||
let user: Option<UsersItemDto> = result.take(0)?;
|
||||
|
||||
user.ok_or_else(|| format!("User not found for email: {}", email).into())
|
||||
pub struct AuthRepository<'a> {
|
||||
state: &'a AppState,
|
||||
}
|
||||
|
||||
pub async fn query_create_user(
|
||||
data: AuthRegisterRequestDto,
|
||||
state: &AppState,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
let db = &state.surrealdb;
|
||||
impl<'a> AuthRepository<'a> {
|
||||
pub fn new(state: &'a AppState) -> Self {
|
||||
Self { state }
|
||||
}
|
||||
|
||||
let _record: Option<UsersItemDto> =
|
||||
db.create(("app_users", &data.email)).content(data).await?;
|
||||
pub async fn query_user_by_email(
|
||||
&self,
|
||||
email: String,
|
||||
) -> Result<AuthRegisterRequestDto, Box<dyn Error>> {
|
||||
let db = &self.state.surrealdb;
|
||||
|
||||
Ok("Success create user".into())
|
||||
let result = db.select((ResourceEnum::Users.to_string(), email)).await?;
|
||||
|
||||
match result {
|
||||
Some(user) => Ok(user),
|
||||
None => Err("User not found for email".into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn query_create_user(
|
||||
&self,
|
||||
data: AuthRegisterRequestDto,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
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 => Err("Failed to create user".into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +1,115 @@
|
||||
use axum::{http::StatusCode, response::Response};
|
||||
use redis::Commands;
|
||||
|
||||
use super::{
|
||||
query_create_user, query_user_by_email, AuthLoginRequestDto,
|
||||
AuthRegisterRequestDto,
|
||||
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,
|
||||
};
|
||||
use crate::{common_response, hash_password, verify_password, AppState};
|
||||
|
||||
pub async fn mutation_login(
|
||||
payload: AuthLoginRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response {
|
||||
match query_user_by_email(payload.email, state).await {
|
||||
Ok(user) => {
|
||||
let is_password_correct =
|
||||
verify_password(&payload.password, &user.password).unwrap_or(false);
|
||||
pub struct AuthService;
|
||||
|
||||
if is_password_correct {
|
||||
common_response(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Email or password not correct",
|
||||
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",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
common_response(StatusCode::OK, "Success Login")
|
||||
}
|
||||
Err(err) => common_response(StatusCode::UNAUTHORIZED, &err.to_string()),
|
||||
}
|
||||
}
|
||||
let new_user = AuthRegisterRequestDto {
|
||||
email: payload.email,
|
||||
password: hashed_password,
|
||||
fullname: payload.fullname,
|
||||
};
|
||||
|
||||
pub async fn mutation_register(
|
||||
payload: AuthRegisterRequestDto,
|
||||
state: &AppState,
|
||||
) -> Response {
|
||||
if query_user_by_email(payload.email.clone(), state)
|
||||
.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",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let new_user = AuthRegisterRequestDto {
|
||||
email: payload.email,
|
||||
password: hashed_password,
|
||||
fullname: payload.fullname,
|
||||
};
|
||||
|
||||
match query_create_user(new_user, state).await {
|
||||
Ok(_) => common_response(StatusCode::CREATED, "Registration successful"),
|
||||
Err(err) => {
|
||||
common_response(StatusCode::INTERNAL_SERVER_ERROR, &err.to_string())
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,5 +5,4 @@ use utoipa::ToSchema;
|
||||
pub struct UsersItemDto {
|
||||
pub email: String,
|
||||
pub fullname: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ use crate::SurrealClient;
|
||||
use surrealdb::engine::remote::http::{Client, Http};
|
||||
use surrealdb::{Result, Surreal};
|
||||
|
||||
pub mod resource;
|
||||
pub use resource::*;
|
||||
|
||||
pub async fn surrealdb_init() -> Result<SurrealClient> {
|
||||
let db = Surreal::<Client>::init();
|
||||
db.connect::<Http>("localhost:8000").await?;
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ResourceEnum {
|
||||
Users,
|
||||
Roles,
|
||||
Permissions,
|
||||
}
|
||||
|
||||
impl fmt::Display for ResourceEnum {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let str = match self {
|
||||
ResourceEnum::Users => "app_users",
|
||||
ResourceEnum::Roles => "app_roles",
|
||||
ResourceEnum::Permissions => "app_permissions",
|
||||
};
|
||||
write!(f, "{}", str)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user