feat(auth): Update Google OAuth integration to enhance response structure and improve callback handling

This commit is contained in:
MythEclipse
2025-08-11 23:47:15 +07:00
parent d7178c792d
commit 1e25a5b496
5 changed files with 338 additions and 45 deletions
@@ -1,18 +1,15 @@
use axum::{
extract::{Query, State},
response::{IntoResponse, Redirect},
response::Redirect,
routing::get,
Json, Router,
};
use axum::http::StatusCode;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use std::sync::Arc;
use imphnen_libs::enviroment::ENV; // Import ENV
use crate::v1::auth::google::google_oauth_service::{AuthRequest, GoogleOauthService, GoogleOauthServiceImpl};
use crate::v1::auth::auth_service::AuthServiceTrait;
use crate::v1::users::users_service::UsersServiceTrait;
use imphnen_entities::error_dto::error::Error;
use crate::v1::auth::AuthLoginResponsetDto;
@@ -74,8 +71,12 @@ where
}
pub async fn google_oauth_callback(&self, auth_request: AuthRequest) -> Result<Json<AuthLoginResponsetDto>, Error> {
let response = self.google_oauth_service.google_oauth_callback(auth_request).await?;
Ok(Json(response))
let (user, token) = self.google_oauth_service.google_oauth_callback(auth_request).await?;
let auth_response = AuthLoginResponsetDto {
user,
token,
};
Ok(Json(auth_response))
}
}
@@ -9,7 +9,7 @@ use oauth2::url::Url;
use imphnen_entities::error_dto::error::Error;
use imphnen_libs::{jsonwebtoken::{encode_access_token, encode_refresh_token}, enviroment::Env};
use crate::v1::auth::{AuthLoginResponsetDto, TokenDto};
use crate::v1::auth::TokenDto;
use crate::v1::auth::auth_service::AuthServiceTrait;
use crate::v1::users::users_dto::{UsersCreateRequestDto, UsersDetailItemDto};
use crate::v1::users::users_service::UsersServiceTrait;
@@ -28,14 +28,15 @@ pub trait GoogleOauthService<A: AuthServiceTrait + Send + Sync + 'static, U: Use
fn with_services(auth_service: A, users_service: U, env: &'static Env) -> Self;
fn google_oauth_client(&self) -> BasicClient;
fn generate_auth_url(&self) -> (Url, CsrfToken);
async fn google_oauth_callback(&self, auth_request: AuthRequest) -> Result<AuthLoginResponsetDto, Error>;
async fn google_oauth_callback(&self, auth_request: AuthRequest) -> Result<(UsersDetailItemDto, TokenDto), Error>; // Changed return type
}
#[derive(Clone)]
pub struct GoogleOauthServiceImpl<A: AuthServiceTrait, U: UsersServiceTrait> {
auth_service: A,
users_service: U,
env: &'static Env,
#[allow(dead_code)]
auth_service: A,
}
impl GoogleOauthServiceImpl<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService> {
@@ -88,7 +89,7 @@ where
.url()
}
async fn google_oauth_callback(&self, auth_request: AuthRequest) -> Result<AuthLoginResponsetDto, Error> {
async fn google_oauth_callback(&self, auth_request: AuthRequest) -> Result<(UsersDetailItemDto, TokenDto), Error> {
let client = self.google_oauth_client();
let token_response = client
@@ -134,13 +135,11 @@ where
let refresh_token = encode_refresh_token(user.email.clone())
.map_err(|e| Error::Db(format!("Failed to generate refresh token: {}", e)))?;
let response = AuthLoginResponsetDto {
user: user.clone(), // Cloned the user to satisfy potential ownership issues
token: TokenDto {
access_token,
refresh_token,
},
let token_dto = TokenDto {
access_token,
refresh_token,
};
Ok(response)
Ok((user, token_dto))
}
}