Brozy123 commited on
Commit
b3731eb
·
verified ·
1 Parent(s): 8e66f1a

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +26 -9
server.js CHANGED
@@ -75,11 +75,19 @@ function hasDashboardPermissions(permissionBits) {
75
  }
76
 
77
  // --- Helper: Get redirect URI ---
78
- function getRedirectUri() {
79
  if (REDIRECT_URI) return REDIRECT_URI;
 
 
 
 
 
 
 
 
 
80
  const spaceId = process.env.SPACE_ID;
81
  if (spaceId) {
82
- // If SPACE_ID is user/name, it becomes user-name.hf.space
83
  const host = spaceId.includes('/') ? spaceId.replace('/', '-').toLowerCase() : spaceId.toLowerCase();
84
  return `https://${host}.hf.space/callback`;
85
  }
@@ -121,8 +129,7 @@ app.get('/', (req, res) => {
121
  // If we get an OAuth code on the root route, handle it using the root as the redirect_uri
122
  if (req.query.code) {
123
  console.log("[Dashboard] Received OAuth code on root route, handling...");
124
- const rootUri = req.protocol + '://' + req.get('host') + '/';
125
- return handleOAuthCallback(req, res, rootUri);
126
  }
127
  res.sendFile(path.join(__dirname, 'dashboard.html'));
128
  });
@@ -148,7 +155,8 @@ app.get('/healthz', (req, res) => {
148
  // --- Discord OAuth2: Step 1 — Redirect to Discord ---
149
  app.get('/auth/login', (req, res) => {
150
  const clientId = getClientId();
151
- const redirect = encodeURIComponent(getRedirectUri());
 
152
  const url = `https://discord.com/api/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirect}&response_type=code&scope=identify%20guilds`;
153
  res.redirect(url);
154
  });
@@ -192,11 +200,20 @@ async function fetchWithRetry(url, options, retries = 2) {
192
  // --- Discord OAuth2: Step 2 — Callback from Discord ---
193
  // Support both /callback and /auth/callback to match any Discord redirect config
194
  async function handleOAuthCallback(req, res, overrideRedirectUri) {
195
- const { code } = req.query;
196
- if (!code) return res.redirect('/?error=no_code');
 
 
 
 
 
 
 
 
 
197
 
198
- const usedRedirectUri = overrideRedirectUri || getRedirectUri();
199
- console.log(`[Dashboard] Handling OAuth callback with redirect_uri: ${usedRedirectUri}`);
200
 
201
  if (!CLIENT_SECRET) {
202
  console.error('[Dashboard] CRITICAL: CLIENT_SECRET is missing from environment variables.');
 
75
  }
76
 
77
  // --- Helper: Get redirect URI ---
78
+ function getRedirectUri(req) {
79
  if (REDIRECT_URI) return REDIRECT_URI;
80
+
81
+ // Fallback: If we have a request object, use it to build the URI dynamically
82
+ if (req) {
83
+ const host = req.get('host');
84
+ const protocol = req.protocol === 'http' && host.includes('.hf.space') ? 'https' : req.protocol;
85
+ const path = req.path === '/' ? '/' : '/callback';
86
+ return `${protocol}://${host}${path}`;
87
+ }
88
+
89
  const spaceId = process.env.SPACE_ID;
90
  if (spaceId) {
 
91
  const host = spaceId.includes('/') ? spaceId.replace('/', '-').toLowerCase() : spaceId.toLowerCase();
92
  return `https://${host}.hf.space/callback`;
93
  }
 
129
  // If we get an OAuth code on the root route, handle it using the root as the redirect_uri
130
  if (req.query.code) {
131
  console.log("[Dashboard] Received OAuth code on root route, handling...");
132
+ return handleOAuthCallback(req, res, getRedirectUri(req));
 
133
  }
134
  res.sendFile(path.join(__dirname, 'dashboard.html'));
135
  });
 
155
  // --- Discord OAuth2: Step 1 — Redirect to Discord ---
156
  app.get('/auth/login', (req, res) => {
157
  const clientId = getClientId();
158
+ const redirect = encodeURIComponent(getRedirectUri(req));
159
+ console.log(`[Dashboard] Starting OAuth login. Redirect URI: ${getRedirectUri(req)}`);
160
  const url = `https://discord.com/api/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirect}&response_type=code&scope=identify%20guilds`;
161
  res.redirect(url);
162
  });
 
200
  // --- Discord OAuth2: Step 2 — Callback from Discord ---
201
  // Support both /callback and /auth/callback to match any Discord redirect config
202
  async function handleOAuthCallback(req, res, overrideRedirectUri) {
203
+ const { code, error, error_description } = req.query;
204
+
205
+ if (error) {
206
+ console.error(`[Dashboard] Discord returned error: ${error} (${error_description})`);
207
+ return res.redirect(`/?error=${error}`);
208
+ }
209
+
210
+ if (!code) {
211
+ console.warn(`[Dashboard] No code found in query parameters. URL: ${req.originalUrl}`);
212
+ return res.redirect('/?error=no_code');
213
+ }
214
 
215
+ const usedRedirectUri = overrideRedirectUri || getRedirectUri(req);
216
+ console.log(`[Dashboard] Handling OAuth callback. Code: ${code.substring(0, 5)}... Redirect URI: ${usedRedirectUri}`);
217
 
218
  if (!CLIENT_SECRET) {
219
  console.error('[Dashboard] CRITICAL: CLIENT_SECRET is missing from environment variables.');