feat(auth): Update Google OAuth integration to enhance response structure and improve callback handling
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::{
|
||||
UsersActiveInactiveRequestDto, UsersCreateRequestDto, UsersDetailItemDto,
|
||||
UsersActiveInactiveRequestDto, UsersCreateRequestDto,
|
||||
UsersSetNewPasswordRequestDto, UsersUpdateRequestDto,
|
||||
};
|
||||
use crate::{
|
||||
@@ -11,12 +11,12 @@ use crate::{
|
||||
};
|
||||
use axum::http::HeaderMap;
|
||||
use axum::{http::StatusCode, response::Response};
|
||||
use imphnen_libs::{ResourceEnum, hash_password, verify_password};
|
||||
use imphnen_libs::{ResourceEnum, hash_password, verify_password, surrealdb_init_ws, surrealdb_init_mem};
|
||||
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};
|
||||
use crate::v1::users::users_dto::{UsersDetailItemDto as UserDto, UsersCreateRequestDto as CreateUserDto};
|
||||
|
||||
#[async_trait]
|
||||
pub trait UsersServiceTrait: Send + Sync + 'static {
|
||||
@@ -250,10 +250,16 @@ impl UsersServiceTrait for UsersService {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
async fn get_user_by_email(&self, email: &str) -> Result<Option<UserDto>> {
|
||||
let surrealdb_ws = surrealdb_init_ws().await
|
||||
.map_err(|e| anyhow::anyhow!("Failed to initialize websocket database: {}", e))?;
|
||||
let surrealdb_mem = surrealdb_init_mem().await
|
||||
.map_err(|e| anyhow::anyhow!("Failed to initialize memory database: {}", e))?;
|
||||
|
||||
let state = AppState {
|
||||
surrealdb_ws: todo!(),
|
||||
surrealdb_mem: todo!(),
|
||||
surrealdb_ws,
|
||||
surrealdb_mem,
|
||||
};
|
||||
let repo = UsersRepository::new(&state);
|
||||
let user = repo.query_user_by_email(email.to_string()).await;
|
||||
@@ -264,12 +270,19 @@ impl UsersServiceTrait for UsersService {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
async fn create_user_by_dto(&self, new_user: CreateUserDto) -> Result<UserDto> {
|
||||
let surrealdb_ws = surrealdb_init_ws().await
|
||||
.map_err(|e| anyhow::anyhow!("Failed to initialize websocket database: {}", e))?;
|
||||
let surrealdb_mem = surrealdb_init_mem().await
|
||||
.map_err(|e| anyhow::anyhow!("Failed to initialize memory database: {}", e))?;
|
||||
|
||||
let state = AppState {
|
||||
surrealdb_ws: todo!(),
|
||||
surrealdb_mem: todo!(),
|
||||
surrealdb_ws,
|
||||
surrealdb_mem,
|
||||
};
|
||||
let repo = UsersRepository::new(&state);
|
||||
let email_clone = new_user.email.clone(); // Store email before moving new_user
|
||||
let user_schema = UsersSchema {
|
||||
email: new_user.email,
|
||||
password: new_user.password, // No unwrap_or_default needed
|
||||
@@ -280,9 +293,9 @@ impl UsersServiceTrait for UsersService {
|
||||
..Default::default()
|
||||
};
|
||||
match repo.query_create_user(user_schema).await {
|
||||
Ok(msg) => { // msg is String, not UsersDetailQueryDto
|
||||
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
|
||||
let created_user = repo.query_user_by_email(email_clone).await?; // Use cloned email
|
||||
Ok(UserDto::from(&created_user)) // Corrected to use UserDto::from by reference
|
||||
},
|
||||
Err(e) => Err(anyhow::anyhow!(e.to_string())),
|
||||
|
||||
Reference in New Issue
Block a user