From 8abf3d3818624638b2963381b6ad067ffbe6b4dd Mon Sep 17 00:00:00 2001 From: Rafi Arrafif Date: Thu, 14 Aug 2025 23:43:16 +0700 Subject: [PATCH] :necktie: (oauth) add callback URI option for google Adding a callback option to the Google OAuth provider used to define the OAuth callback URI. If not defined, the callback URI will use the default value in the .env file. --- .env.example | 2 +- src/modules/auth/controllers/googleRequest.controller.ts | 6 ++++-- src/modules/auth/providers/google.provider.ts | 7 ++++--- src/modules/auth/services/http/googleRequest.service.ts | 4 ++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index 2255843..5f287b2 100644 --- a/.env.example +++ b/.env.example @@ -38,4 +38,4 @@ GITHUB_DEFAULT_CALLBACK= GOOGLE_CLIENT_ID= GOOGLE_CLIENT_SECRET= -GOOGLE_CLIENT_CALLBACK= \ No newline at end of file +GOOGLE_DEFAULT_CALLBACK= \ No newline at end of file diff --git a/src/modules/auth/controllers/googleRequest.controller.ts b/src/modules/auth/controllers/googleRequest.controller.ts index bba0a05..8b11796 100644 --- a/src/modules/auth/controllers/googleRequest.controller.ts +++ b/src/modules/auth/controllers/googleRequest.controller.ts @@ -3,9 +3,11 @@ import { mainErrorHandler } from "../../../helpers/error/handler"; import { googleRequestService } from "../services/http/googleRequest.service"; import { returnReadResponse } from "../../../helpers/callback/httpResponse"; -export const googleRequestController = async (ctx: Context) => { +export const googleRequestController = async ( + ctx: Context & { query: { callback?: string } } +) => { try { - const loginUrl = await googleRequestService(); + const loginUrl = await googleRequestService(ctx.query.callback); return returnReadResponse( ctx.set, 200, diff --git a/src/modules/auth/providers/google.provider.ts b/src/modules/auth/providers/google.provider.ts index 9bb0288..3bb5d39 100644 --- a/src/modules/auth/providers/google.provider.ts +++ b/src/modules/auth/providers/google.provider.ts @@ -1,10 +1,11 @@ import { Google } from "arctic"; -export const googleProvider = () => { - const redirectURI = `${process.env.APP_PROTOCOL}://${process.env.APP_DOMAIN}${process.env.GOOGLE_CLIENT_CALLBACK}`; +export const googleProvider = ( + callbackURI = `${process.env.APP_PROTOCOL}://${process.env.APP_DOMAIN}${process.env.GOOGLE_DEFAULT_CALLBACK}` +) => { return new Google( process.env.GOOGLE_CLIENT_ID!, process.env.GOOGLE_CLIENT_SECRET!, - redirectURI + callbackURI ); }; diff --git a/src/modules/auth/services/http/googleRequest.service.ts b/src/modules/auth/services/http/googleRequest.service.ts index 2f58efc..512e5ad 100644 --- a/src/modules/auth/services/http/googleRequest.service.ts +++ b/src/modules/auth/services/http/googleRequest.service.ts @@ -3,9 +3,9 @@ import { AppError } from "../../../../helpers/error/instances/app"; import { googleProvider } from "../../providers/google.provider"; import { redis } from "../../../../utils/databases/redis/connection"; -export const googleRequestService = async () => { +export const googleRequestService = async (callbackURI?: string) => { try { - const google = googleProvider(); + const google = googleProvider(callbackURI); const state = arctic.generateState(); const codeVerifier = arctic.generateCodeVerifier(); const scopes = ["openid", "profile", "email"];