feat(auth): Implement Google OAuth 2.0 integration with user creation and JWT generation

- Added Google OAuth controller and service to handle authentication via Google.
- Introduced DTOs for Google user and token responses.
- Updated AuthService and UsersService traits to support new Google OAuth functionality.
- Implemented logic to create a new user if they do not exist in the system after Google authentication.
- Enhanced existing user retrieval and JWT generation upon successful login.
- Added tests for Google OAuth flow, including login redirection and callback handling for both new and existing users.
- Updated environment configuration to include Google OAuth credentials.
This commit is contained in:
MythEclipse
2025-08-11 22:31:51 +07:00
parent c6a95c1231
commit cda950ed7e
22 changed files with 1144 additions and 104 deletions
+5 -4
View File
@@ -1,9 +1,9 @@
use crate::{AppState, MetaRequestDto, v1::users_service::UsersService};
use crate::{AppState, MetaRequestDto};
use crate::{
MessageResponseDto, PermissionsEnum, ResponseListSuccessDto, ResponseSuccessDto,
UsersCreateRequestDto, UsersDetailItemDto, permissions_guard,
};
use axum::extract::{Path, Query};
use axum::extract::{Path}; // Removed Query
use axum::http::HeaderMap;
use axum::response::IntoResponse;
use axum::{Extension, Json};
@@ -11,6 +11,7 @@ use axum::{Extension, Json};
use super::{
UsersActiveInactiveRequestDto, UsersListItemDto, UsersUpdateRequestDto,
};
use crate::v1::users::users_service::{UsersServiceTrait, UsersService};
#[utoipa::path(
get,
@@ -35,7 +36,7 @@ use super::{
pub async fn get_user_list(
headers: HeaderMap,
Extension(state): Extension<AppState>,
Query(meta): Query<MetaRequestDto>,
Json(meta): Json<MetaRequestDto>,
) -> impl IntoResponse {
match permissions_guard(
&headers,
@@ -178,7 +179,7 @@ pub async fn put_update_user_me(
Json(payload): Json<UsersUpdateRequestDto>,
) -> impl IntoResponse {
match permissions_guard(&headers, state.clone(), vec![]).await {
Ok(_) => UsersService::update_user_me(&state, headers, payload).await,
Ok(_) => UsersService::update_user_me(headers, &state, payload).await,
Err(response) => response,
}
}
+20 -3
View File
@@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use surrealdb::sql::Thing;
use utoipa::ToSchema;
use validator::Validate;
use crate::UsersSchema; // Import UsersSchema
lazy_static! {
static ref PASSWORD_REGEX: regex::Regex =
@@ -96,14 +97,14 @@ pub struct UsersDetailItemDto {
}
impl UsersDetailItemDto {
pub fn from(dto: &UsersDetailQueryDto) -> Self {
pub fn from(dto: &UsersDetailQueryDto) -> Self { // Reverted to taking a reference
Self {
id: dto.id.id.to_raw().clone(),
role: RolesDetailItemDto::from(&dto.role),
fullname: dto.fullname.clone(),
email: dto.email.clone(),
avatar: dto.avatar.clone(),
phone_number: dto.phone_number.clone(),
phone_number: dto.phone_number.clone(), // Corrected from dto.phone.clone()
is_active: dto.is_active,
gender: dto.gender.clone(),
birthdate: dto.birthdate.clone(),
@@ -111,6 +112,22 @@ impl UsersDetailItemDto {
updated_at: dto.updated_at.clone(),
}
}
pub fn from_schema(schema: &UsersSchema) -> Self {
Self {
id: schema.id.id.to_raw(),
role: RolesDetailItemDto::default(), // Placeholder, role needs to be fetched
fullname: schema.fullname.clone(),
email: schema.email.clone(),
avatar: schema.avatar.clone(),
phone_number: schema.phone_number.clone(),
is_active: schema.is_active,
gender: schema.gender.clone(),
birthdate: schema.birthdate.clone(),
created_at: schema.created_at.clone(),
updated_at: schema.updated_at.clone(),
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
@@ -131,7 +148,7 @@ pub struct UsersListQueryDto {
pub id: Thing,
pub role: RolesDetailQueryDto,
pub fullname: String,
pub email: String,
pub email: String, // Corrected from pub pub email: String,
pub avatar: Option<String>,
pub phone_number: String,
pub is_active: bool,
+75 -15
View File
@@ -14,12 +14,33 @@ use axum::{http::StatusCode, response::Response};
use imphnen_libs::{ResourceEnum, hash_password, verify_password};
use imphnen_utils::make_thing;
use uuid::Uuid;
use anyhow::Result;
use async_trait::async_trait;
use crate::v1::users::users_dto::{UsersDetailItemDto as UserDto, UsersCreateRequestDto as CreateUserDto, UsersDetailQueryDto};
#[async_trait]
pub trait UsersServiceTrait: Send + Sync + 'static {
async fn get_user_list(state: &AppState, meta: MetaRequestDto) -> Response;
async fn get_user_by_id(state: &AppState, id: String) -> Response;
async fn get_user_me(headers: HeaderMap, state: &AppState) -> Response;
async fn create_user(state: &AppState, new_user: UsersCreateRequestDto) -> Response;
async fn update_user(state: &AppState, id: String, user: UsersUpdateRequestDto) -> Response;
async fn update_user_me(headers: HeaderMap, state: &AppState, user: UsersUpdateRequestDto) -> Response;
async fn set_user_active_status(state: &AppState, id: String, payload: UsersActiveInactiveRequestDto) -> Response;
async fn update_user_password(state: &AppState, email: String, payload: UsersSetNewPasswordRequestDto) -> Response;
async fn get_user_by_mentor_id(state: &AppState, mentor_id: String) -> Response;
async fn delete_user(state: &AppState, id: String) -> Response;
async fn get_user_by_email(&self, email: &str) -> Result<Option<UserDto>>;
async fn create_user_by_dto(&self, new_user: CreateUserDto) -> Result<UserDto>;
}
#[derive(Clone)]
pub struct UsersService;
impl UsersService {
pub async fn get_user_list(state: &AppState, meta: MetaRequestDto) -> Response {
#[async_trait]
impl UsersServiceTrait for UsersService {
async fn get_user_list(state: &AppState, meta: MetaRequestDto) -> Response {
let repo = UsersRepository::new(state);
match repo.query_user_list(meta).await {
Ok(data) => {
@@ -33,7 +54,7 @@ impl UsersService {
}
}
pub async fn get_user_by_id(state: &AppState, id: String) -> Response {
async fn get_user_by_id(state: &AppState, id: String) -> Response {
if Uuid::parse_str(&id).is_err() {
return common_response(StatusCode::BAD_REQUEST, "Invalid User ID format");
}
@@ -41,14 +62,14 @@ impl UsersService {
let thing_id = make_thing(&ResourceEnum::Users.to_string(), &id);
match repo.query_user_by_id(&thing_id).await {
Ok(user) if !user.is_deleted => success_response(ResponseSuccessDto {
data: UsersDetailItemDto::from(&user),
data: UserDto::from(&user), // Corrected to use UserDto::from by reference
}),
Ok(_) => common_response(StatusCode::NOT_FOUND, "User not found"),
Err(e) => common_response(StatusCode::NOT_FOUND, &e.to_string()),
}
}
pub async fn get_user_me(headers: HeaderMap, state: &AppState) -> Response {
async fn get_user_me(headers: HeaderMap, state: &AppState) -> Response {
let repo = UsersRepository::new(state);
let email = match extract_email(&headers) {
Some(email) => email,
@@ -56,14 +77,14 @@ impl UsersService {
};
match repo.query_user_by_email(email).await {
Ok(user) if !user.is_deleted => success_response(ResponseSuccessDto {
data: UsersDetailItemDto::from(&user),
data: UserDto::from(&user), // Corrected to use UserDto::from by reference
}),
Ok(_) => common_response(StatusCode::NOT_FOUND, "User not found"),
Err(e) => common_response(StatusCode::NOT_FOUND, &e.to_string()),
}
}
pub async fn create_user(
async fn create_user(
state: &AppState,
new_user: UsersCreateRequestDto,
) -> Response {
@@ -86,7 +107,7 @@ impl UsersService {
}
}
pub async fn update_user(
async fn update_user(
state: &AppState,
id: String,
user: UsersUpdateRequestDto,
@@ -105,9 +126,9 @@ impl UsersService {
}
}
pub async fn update_user_me(
state: &AppState,
async fn update_user_me(
headers: HeaderMap,
state: &AppState,
user: UsersUpdateRequestDto,
) -> Response {
let repo = UsersRepository::new(state);
@@ -129,7 +150,7 @@ impl UsersService {
}
}
pub async fn set_user_active_status(
async fn set_user_active_status(
state: &AppState,
id: String,
payload: UsersActiveInactiveRequestDto,
@@ -156,7 +177,7 @@ impl UsersService {
}
}
pub async fn update_user_password(
async fn update_user_password(
state: &AppState,
email: String,
payload: UsersSetNewPasswordRequestDto,
@@ -199,7 +220,7 @@ impl UsersService {
}
}
pub async fn get_user_by_mentor_id(
async fn get_user_by_mentor_id(
state: &AppState,
mentor_id: String,
) -> Response {
@@ -207,14 +228,14 @@ impl UsersService {
let thing_id = make_thing(&ResourceEnum::Mentors.to_string(), &mentor_id);
match repo.query_user_by_id(&thing_id).await {
Ok(user) if !user.is_deleted => success_response(ResponseSuccessDto {
data: UsersDetailItemDto::from(&user),
data: UserDto::from(&user), // Corrected to use UserDto::from by reference
}),
Ok(_) => common_response(StatusCode::NOT_FOUND, "User not found"),
Err(e) => common_response(StatusCode::NOT_FOUND, &e.to_string()),
}
}
pub async fn delete_user(state: &AppState, id: String) -> Response {
async fn delete_user(state: &AppState, id: String) -> Response {
if Uuid::parse_str(&id).is_err() {
return common_response(StatusCode::BAD_REQUEST, "Invalid User ID format");
}
@@ -228,4 +249,43 @@ impl UsersService {
Err(e) => common_response(StatusCode::BAD_REQUEST, &e.to_string()),
}
}
async fn get_user_by_email(&self, email: &str) -> Result<Option<UserDto>> {
let state = AppState {
surrealdb_ws: todo!(),
surrealdb_mem: todo!(),
};
let repo = UsersRepository::new(&state);
let user = repo.query_user_by_email(email.to_string()).await;
match user {
Ok(u) => Ok(Some(UserDto::from(&u))), // Corrected to use UserDto::from by reference
Err(e) if e.to_string().contains("User not found") => Ok(None),
Err(e) => Err(anyhow::anyhow!(e.to_string())),
}
}
async fn create_user_by_dto(&self, new_user: CreateUserDto) -> Result<UserDto> {
let state = AppState {
surrealdb_ws: todo!(),
surrealdb_mem: todo!(),
};
let repo = UsersRepository::new(&state);
let user_schema = UsersSchema {
email: new_user.email,
password: new_user.password, // No unwrap_or_default needed
fullname: new_user.fullname,
phone_number: new_user.phone_number, // No unwrap_or_default needed
is_active: new_user.is_active, // No unwrap_or needed
role: make_thing(&ResourceEnum::Roles.to_string(), &new_user.role_id),
..Default::default()
};
match repo.query_create_user(user_schema).await {
Ok(msg) => { // msg is String, not UsersDetailQueryDto
// Re-fetch the created user to get the full UsersDetailQueryDto
let created_user = repo.query_user_by_email(new_user.email.clone()).await?; // Cloned email
Ok(UserDto::from(&created_user)) // Corrected to use UserDto::from by reference
},
Err(e) => Err(anyhow::anyhow!(e.to_string())),
}
}
}