feat(auth): Refactor Google OAuth service and controller to utilize environment variables and enhance response structure
This commit is contained in:
@@ -7,35 +7,38 @@ use axum::{
|
||||
use axum::http::StatusCode;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
use std::sync::Arc; // Import Arc
|
||||
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;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct GoogleAuthUrlResponse {
|
||||
pub authorize_url: String,
|
||||
}
|
||||
|
||||
pub struct GoogleOauthController<T> { // Generic over T
|
||||
pub struct GoogleOauthController<T> {
|
||||
google_oauth_service: T,
|
||||
}
|
||||
|
||||
// Concrete implementation for new()
|
||||
impl GoogleOauthController<GoogleOauthServiceImpl<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService>> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
google_oauth_service: GoogleOauthServiceImpl::<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService>::new(), // Explicitly specify type parameters
|
||||
}
|
||||
let google_oauth_service = GoogleOauthServiceImpl::<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService>::with_services(
|
||||
crate::v1::auth::auth_service::AuthService {},
|
||||
crate::v1::users::users_service::UsersService {},
|
||||
&ENV, // Pass a reference to the global ENV static
|
||||
);
|
||||
Self::with_service(google_oauth_service)
|
||||
}
|
||||
}
|
||||
|
||||
// Generic implementation for with_service and get_routes
|
||||
impl<T> GoogleOauthController<T>
|
||||
where
|
||||
T: GoogleOauthService<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService> + Clone + Send + Sync + 'static, // Explicitly constrain T
|
||||
T: GoogleOauthService<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService> + Clone + Send + Sync + 'static,
|
||||
{
|
||||
pub fn with_service(google_oauth_service: T) -> Self {
|
||||
Self {
|
||||
@@ -43,7 +46,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_routes(&self) -> Router { // Take self by reference
|
||||
pub fn get_routes(&self) -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/google/login",
|
||||
@@ -62,7 +65,7 @@ where
|
||||
},
|
||||
),
|
||||
)
|
||||
.with_state(Arc::new(self.clone())) // Pass an Arc clone of self to with_state
|
||||
.with_state(Arc::new(self.clone()))
|
||||
}
|
||||
|
||||
pub async fn google_oauth_login(&self) -> Result<Redirect, Error> {
|
||||
@@ -70,16 +73,15 @@ where
|
||||
Ok(Redirect::to(authorize_url.as_str()))
|
||||
}
|
||||
|
||||
pub async fn google_oauth_callback(&self, auth_request: AuthRequest) -> Result<impl IntoResponse + use<T>, Error> {
|
||||
let token = self.google_oauth_service.google_oauth_callback(auth_request).await?;
|
||||
Ok((StatusCode::OK, Json(serde_json::json!({"token": token}))))
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
// Clone implementation
|
||||
impl<T> Clone for GoogleOauthController<T>
|
||||
where
|
||||
T: GoogleOauthService<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService> + Clone, // Explicitly constrain T
|
||||
T: GoogleOauthService<crate::v1::auth::auth_service::AuthService, crate::v1::users::users_service::UsersService> + Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self::with_service(self.google_oauth_service.clone())
|
||||
|
||||
Reference in New Issue
Block a user