Spaces:
Runtime error
Runtime error
Update server.js
Browse files
server.js
CHANGED
|
@@ -5,9 +5,9 @@ const crypto = require('crypto');
|
|
| 5 |
const app = express();
|
| 6 |
const port = process.env.PORT || 7860;
|
| 7 |
|
| 8 |
-
// Discord OAuth2 Config
|
| 9 |
-
const CLIENT_SECRET = process.env.CLIENT_SECRET || '';
|
| 10 |
-
const REDIRECT_URI = process.env.REDIRECT_URI || '';
|
| 11 |
|
| 12 |
// Store reference to the Discord client (set from index.js)
|
| 13 |
let discordClient = null;
|
|
@@ -42,13 +42,13 @@ app.use(express.json());
|
|
| 42 |
function getRedirectUri() {
|
| 43 |
if (REDIRECT_URI) return REDIRECT_URI;
|
| 44 |
const spaceId = process.env.SPACE_ID;
|
| 45 |
-
if (spaceId) return `https://${spaceId.replace('/', '-')}.hf.space/
|
| 46 |
-
return `http://localhost:${port}/
|
| 47 |
}
|
| 48 |
|
| 49 |
// --- Helper: Get Client ID from bot ---
|
| 50 |
function getClientId() {
|
| 51 |
-
return discordClient?.user?.id || process.env.CLIENT_ID || '';
|
| 52 |
}
|
| 53 |
|
| 54 |
// --- Helper: Parse session cookie ---
|
|
@@ -95,7 +95,8 @@ app.get('/auth/login', (req, res) => {
|
|
| 95 |
});
|
| 96 |
|
| 97 |
// --- Discord OAuth2: Step 2 — Callback from Discord ---
|
| 98 |
-
|
|
|
|
| 99 |
const { code } = req.query;
|
| 100 |
if (!code) return res.redirect('/?error=no_code');
|
| 101 |
|
|
|
|
| 5 |
const app = express();
|
| 6 |
const port = process.env.PORT || 7860;
|
| 7 |
|
| 8 |
+
// Discord OAuth2 Config — reads from multiple possible env var names
|
| 9 |
+
const CLIENT_SECRET = process.env.CLIENT_SECRET || process.env.DISCORD_CLIENT_SECRET || '';
|
| 10 |
+
const REDIRECT_URI = process.env.REDIRECT_URI || process.env.DISCORD_REDIRECT_URL || '';
|
| 11 |
|
| 12 |
// Store reference to the Discord client (set from index.js)
|
| 13 |
let discordClient = null;
|
|
|
|
| 42 |
function getRedirectUri() {
|
| 43 |
if (REDIRECT_URI) return REDIRECT_URI;
|
| 44 |
const spaceId = process.env.SPACE_ID;
|
| 45 |
+
if (spaceId) return `https://${spaceId.replace('/', '-').toLowerCase()}.hf.space/callback`;
|
| 46 |
+
return `http://localhost:${port}/callback`;
|
| 47 |
}
|
| 48 |
|
| 49 |
// --- Helper: Get Client ID from bot ---
|
| 50 |
function getClientId() {
|
| 51 |
+
return discordClient?.user?.id || process.env.CLIENT_ID || process.env.DISCORD_CLIENT_ID || '';
|
| 52 |
}
|
| 53 |
|
| 54 |
// --- Helper: Parse session cookie ---
|
|
|
|
| 95 |
});
|
| 96 |
|
| 97 |
// --- Discord OAuth2: Step 2 — Callback from Discord ---
|
| 98 |
+
// Support both /callback and /auth/callback to match any Discord redirect config
|
| 99 |
+
async function handleOAuthCallback(req, res) {
|
| 100 |
const { code } = req.query;
|
| 101 |
if (!code) return res.redirect('/?error=no_code');
|
| 102 |
|