Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit 019fa00

Browse files
committed
feat: Improve Discord OAuth flow by adding redirect URI validation and enhanced error logging
1 parent 6887dad commit 019fa00

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

dist/controllers/UserController.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,30 @@ let Users = class Users {
100100
let accessToken;
101101
let verifiedUser;
102102
if (provider === "discord") {
103+
const redirectUri = process.env.DISCORD_CALLBACK_URL;
104+
if (!redirectUri) {
105+
await this.createLog(req, "loginOAuth", "users", 500);
106+
return this.sendError(res, 500, "Discord redirect_uri is not set in environment variables");
107+
}
103108
const params = new URLSearchParams({
104109
client_id: process.env.DISCORD_CLIENT_ID,
105110
client_secret: process.env.DISCORD_CLIENT_SECRET,
106111
grant_type: "authorization_code",
107-
code,
108-
redirect_uri: process.env.DISCORD_CALLBACK_URL,
112+
code: code,
113+
redirect_uri: redirectUri,
109114
});
110115
const tokenRes = await fetch("https://discord.com/api/oauth2/token", {
111116
method: "POST",
112117
headers: { "Content-Type": "application/x-www-form-urlencoded" },
113118
body: params.toString(),
114119
});
115-
if (!tokenRes.ok)
116-
return this.sendError(res, 500, "Failed to fetch Discord access token");
120+
if (!tokenRes.ok) {
121+
const errorText = await tokenRes.text();
122+
console.error("Discord token error:", errorText);
123+
console.error("Params sent to Discord:", params.toString());
124+
await this.createLog(req, "loginOAuth", "users", 500);
125+
return this.sendError(res, 500, "Failed to fetch Discord access token: " + errorText);
126+
}
117127
const tokenData = await tokenRes.json();
118128
accessToken = tokenData.access_token;
119129
verifiedUser = await this.verifyDiscordToken(accessToken);

src/controllers/UserController.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,31 @@ export class Users {
9797
let verifiedUser: { id: string; email: string; username: string };
9898

9999
if (provider === "discord") {
100+
const redirectUri = process.env.DISCORD_CALLBACK_URL!;
101+
if (!redirectUri) {
102+
await this.createLog(req, "loginOAuth", "users", 500);
103+
return this.sendError(res, 500, "Discord redirect_uri is not set in environment variables");
104+
}
105+
100106
const params = new URLSearchParams({
101107
client_id: process.env.DISCORD_CLIENT_ID!,
102108
client_secret: process.env.DISCORD_CLIENT_SECRET!,
103109
grant_type: "authorization_code",
104-
code,
105-
redirect_uri: process.env.DISCORD_CALLBACK_URL!,
110+
code: code,
111+
redirect_uri: redirectUri,
106112
});
107113
const tokenRes = await fetch("https://discord.com/api/oauth2/token", {
108114
method: "POST",
109115
headers: { "Content-Type": "application/x-www-form-urlencoded" },
110116
body: params.toString(),
111117
});
112-
if (!tokenRes.ok) return this.sendError(res, 500, "Failed to fetch Discord access token");
118+
if (!tokenRes.ok) {
119+
const errorText = await tokenRes.text();
120+
console.error("Discord token error:", errorText);
121+
console.error("Params sent to Discord:", params.toString());
122+
await this.createLog(req, "loginOAuth", "users", 500);
123+
return this.sendError(res, 500, "Failed to fetch Discord access token: " + errorText);
124+
}
113125
const tokenData = await tokenRes.json();
114126
accessToken = tokenData.access_token;
115127
verifiedUser = await this.verifyDiscordToken(accessToken!);

0 commit comments

Comments
 (0)