diff --git a/.env.example b/.env.example index 3c66340..3ba4dc0 100644 --- a/.env.example +++ b/.env.example @@ -31,7 +31,7 @@ WEBHOOK_URL=https://discord.com/api/webhooks/123456789123456789/a1b2c3d4e5f6g7h8 # ==================== # needs quotes because dotenv limitations -AUTH_URLS={"webapp": "http://localhost/"} +AUTH_URLS={"api": "http://localhost:8000", "webapp": "http://localhost/"} # discord DISCORD_CLIENT_ID=abcdef123456789 diff --git a/src/v2/controller/auth.controller.ts b/src/v2/controller/auth.controller.ts index f38e60f..8162389 100644 --- a/src/v2/controller/auth.controller.ts +++ b/src/v2/controller/auth.controller.ts @@ -40,7 +40,7 @@ export class AuthController extends Controller { params.append("client_secret", process.env.DISCORD_CLIENT_SECRET); params.append("grant_type", "authorization_code"); params.append("code", code); - params.append("redirect_uri", `${this.service.getApiUrl(request)}${request.path}`); + params.append("redirect_uri", `${this.service.targetToURL("api")}${request.path}`); params.append("scope", "identify"); const res = (request).res as ExResponse; diff --git a/src/v2/service/auth.service.ts b/src/v2/service/auth.service.ts index 87a18c8..ee81be3 100644 --- a/src/v2/service/auth.service.ts +++ b/src/v2/service/auth.service.ts @@ -7,10 +7,6 @@ const REDIRECT_URI_PATHNAME = "/callback/"; // REDIRECT URL IS '/auth/discord/callback' export default class AuthService { - getApiUrl(request: ExRequest): string { - return `${request.protocol}://${request.headers.host}`; - } - rawTargetToURL(target: string): string { const url = JSON.parse(process.env.AUTH_URLS)[target]; if (!url) throw new NotFoundError("Target not found!"); @@ -22,10 +18,10 @@ export default class AuthService { } getRedirectURI(request: ExRequest, target: string): string { - const apiOrigin = this.getApiUrl(request); + const apiOrigin = this.targetToURL("api"); const requestPath = request.path; const pathSplit = requestPath.split("/"); pathSplit.pop(); // remove target - return `${apiOrigin.replace(/\/$/, "")}${pathSplit.join("/")}${REDIRECT_URI_PATHNAME}${target}`; + return `${apiOrigin}${pathSplit.join("/")}${REDIRECT_URI_PATHNAME}${target}`; } }