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
+48 -9
View File
@@ -15,10 +15,49 @@ use axum::{http::StatusCode, response::Response};
use surrealdb::Uuid;
use tracing::error;
use async_trait::async_trait;
#[async_trait]
pub trait AuthServiceTrait: Send + Sync + 'static {
async fn mutation_login(
payload: AuthLoginRequestDto,
state: &AppState,
) -> Response;
async fn mutation_mentor_login(
payload: AuthLoginRequestDto,
state: &AppState,
) -> Response;
async fn mutation_register(
payload: AuthRegisterRequestDto,
state: &AppState,
) -> Response;
async fn mutation_resend_otp(
payload: AuthResendOtpRequestDto,
state: &AppState,
) -> Response;
async fn mutation_refresh_token(
payload: AuthRefreshTokenRequestDto,
) -> Response;
async fn mutation_forgot_password(
payload: AuthResendOtpRequestDto,
state: &AppState,
) -> Response;
async fn mutation_verify_email(
payload: AuthVerifyEmailRequestDto,
state: &AppState,
) -> Response;
async fn mutation_new_password(
payload: AuthNewPasswordRequestDto,
state: &AppState,
) -> Response;
}
#[derive(Clone)] // Added Clone derive
pub struct AuthService;
impl AuthService {
pub async fn mutation_login(
#[async_trait]
impl AuthServiceTrait for AuthService {
async fn mutation_login(
payload: AuthLoginRequestDto,
state: &AppState,
) -> Response {
@@ -104,7 +143,7 @@ impl AuthService {
}
}
pub async fn mutation_mentor_login(
async fn mutation_mentor_login(
payload: AuthLoginRequestDto,
state: &AppState,
) -> Response {
@@ -199,7 +238,7 @@ impl AuthService {
}
}
pub async fn mutation_register(
async fn mutation_register(
payload: AuthRegisterRequestDto,
state: &AppState,
) -> Response {
@@ -298,7 +337,7 @@ impl AuthService {
}
}
pub async fn mutation_resend_otp(
async fn mutation_resend_otp(
payload: AuthResendOtpRequestDto,
state: &AppState,
) -> Response {
@@ -335,7 +374,7 @@ impl AuthService {
}
}
pub async fn mutation_refresh_token(
async fn mutation_refresh_token(
payload: AuthRefreshTokenRequestDto,
) -> Response {
if let Err((status, message)) = validate_request(&payload) {
@@ -376,7 +415,7 @@ impl AuthService {
success_response(response)
}
pub async fn mutation_forgot_password(
async fn mutation_forgot_password(
payload: AuthResendOtpRequestDto,
state: &AppState,
) -> Response {
@@ -431,7 +470,7 @@ impl AuthService {
}
}
pub async fn mutation_verify_email(
async fn mutation_verify_email(
payload: AuthVerifyEmailRequestDto,
state: &AppState,
) -> Response {
@@ -478,7 +517,7 @@ impl AuthService {
}
}
pub async fn mutation_new_password(
async fn mutation_new_password(
payload: AuthNewPasswordRequestDto,
state: &AppState,
) -> Response {