Skip to content

Commit

Permalink
quick auth fix
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Mar 3, 2024
1 parent d10554e commit ab08ee0
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/v2/controller/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (<any>request).res as ExResponse;
Expand Down
8 changes: 2 additions & 6 deletions src/v2/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ const REDIRECT_URI_PATHNAME = "/callback/";
// REDIRECT URL IS '<origin>/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!");
Expand All @@ -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}`;
}
}

0 comments on commit ab08ee0

Please sign in to comment.