From c1d5d1692bf573859c6fb5800399fc5703acdf55 Mon Sep 17 00:00:00 2001 From: Rafi Arrafif Date: Thu, 8 Jan 2026 17:22:05 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20new=20route=20to=20ge?= =?UTF-8?q?t=20client=20callback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This new route serves to find the client callback after successfully logging in from the provider page. This is done so that the callback becomes more flexible and can be changed at any time. --- .../getCallbackProviderUrl.controller.ts | 22 +++++++++++++++++++ src/modules/auth/index.ts | 2 ++ .../http/getCallbackProviderUrl.service.ts | 14 ++++++++++++ tsconfig.json | 2 +- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/modules/auth/controllers/getCallbackProviderUrl.controller.ts create mode 100644 src/modules/auth/services/http/getCallbackProviderUrl.service.ts diff --git a/src/modules/auth/controllers/getCallbackProviderUrl.controller.ts b/src/modules/auth/controllers/getCallbackProviderUrl.controller.ts new file mode 100644 index 0000000..59fa9d6 --- /dev/null +++ b/src/modules/auth/controllers/getCallbackProviderUrl.controller.ts @@ -0,0 +1,22 @@ +import { Context } from "elysia"; +import { mainErrorHandler } from "../../../helpers/error/handler"; +import { getCallbackProviderUrlService } from "../services/http/getCallbackProviderUrl.service"; +import { returnReadResponse } from "../../../helpers/callback/httpResponse"; + +export const getCallbackProviderUrlController = async ( + ctx: Context & { params: { name: string } } +) => { + try { + const callbackProviderUrl = await getCallbackProviderUrlService( + ctx.params.name + ); + return returnReadResponse( + ctx.set, + 200, + "The callback URL on the provider has been found.", + callbackProviderUrl + ); + } catch (error) { + return mainErrorHandler(ctx.set, error); + } +}; diff --git a/src/modules/auth/index.ts b/src/modules/auth/index.ts index 87fd29a..3d8fc18 100644 --- a/src/modules/auth/index.ts +++ b/src/modules/auth/index.ts @@ -4,9 +4,11 @@ import { githubCallbackController } from "./controllers/githubCallback.controlle import { googleRequestController } from "./controllers/googleRequest.controller"; import { googleCallbackController } from "./controllers/googleCallback.controller"; import { getOauthProvidersController } from "./controllers/getOauthProviders.controller"; +import { getCallbackProviderUrlController } from "./controllers/getCallbackProviderUrl.controller"; export const authModule = new Elysia({ prefix: "/auth" }) .get("/providers", getOauthProvidersController) + .get("/providers/:name/callback", getCallbackProviderUrlController) .get("/github", githubRequestController) .get("/github/callback", githubCallbackController) .get("/google", googleRequestController) diff --git a/src/modules/auth/services/http/getCallbackProviderUrl.service.ts b/src/modules/auth/services/http/getCallbackProviderUrl.service.ts new file mode 100644 index 0000000..e275be3 --- /dev/null +++ b/src/modules/auth/services/http/getCallbackProviderUrl.service.ts @@ -0,0 +1,14 @@ +import { getOauthProviders } from "../../../../config/oauthProvider"; +import { AppError } from "../../../../helpers/error/instances/app"; + +export const getCallbackProviderUrlService = async (providerName: string) => { + const callbackUrl = getOauthProviders().find( + (provider) => provider.name === providerName + )?.client_callback; + + if (!callbackUrl) { + throw new AppError(404, "The specified provider does not exist."); + } + + return callbackUrl; +}; diff --git a/tsconfig.json b/tsconfig.json index 0606d2d..3803c4f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,7 +25,7 @@ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "Node16" /* Specify what module code is generated. */, + "module": "NodeNext" /* Specify what module code is generated. */, // "rootDir": "./", /* Specify the root folder within your source files. */ "moduleResolution": "node16" /* Specify how TypeScript looks up a file from a given module specifier. */, // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */