👔 (oauth) add callback URI options

provides the option to define a callback URI following a request from the frontend; if not defined, it will
automatically use the backend's default URI.
This commit is contained in:
Rafi Arrafif
2025-08-13 18:51:10 +07:00
parent 89ebfb8aa4
commit 3df1f342fc
4 changed files with 12 additions and 9 deletions

View File

@ -2,12 +2,14 @@ import { Context } from "elysia";
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
import { githubRequestService } from "../services/http/githubRequest.service";
export const githubRequestController = async (ctx: Context) => {
const loginUrl = await githubRequestService();
export const githubRequestController = async (
ctx: Context & { query: { callback?: string } }
) => {
const loginUrl = await githubRequestService(ctx.query.callback);
return returnReadResponse(
ctx.set,
200,
"Login URL generated successfully",
String(loginUrl)
loginUrl
);
};

View File

@ -1,10 +1,11 @@
import { GitHub } from "arctic";
export const githubProvider = () => {
const redirectURI = `${process.env.APP_PROTOCOL}://${process.env.APP_DOMAIN}${process.env.GITHUB_CLIENT_CALLBACK}`;
export const githubProvider = (
callbackURI = `${process.env.APP_PROTOCOL}://${process.env.APP_DOMAIN}${process.env.GITHUB_DEFAULT_CALLBACK}`
) => {
return new GitHub(
process.env.GITHUB_CLIENT_ID!,
process.env.GITHUB_CLIENT_SECRET!,
redirectURI
callbackURI
);
};

View File

@ -2,9 +2,9 @@ import * as arctic from "arctic";
import { githubProvider } from "../../providers/github.provider";
import { AppError } from "../../../../helpers/error/instances/app";
export const githubRequestService = async () => {
export const githubRequestService = async (callbackURI?: string) => {
try {
const github = githubProvider();
const github = githubProvider(callbackURI);
const state = arctic.generateState();
const scopes = ["user:email"];
const url = github.createAuthorizationURL(state, scopes);