GuaccO commited on
Commit
04475cf
·
verified ·
1 Parent(s): f82a422

Connect to github doesnt work

Browse files
Files changed (2) hide show
  1. auth/github/callback.html +31 -0
  2. script.js +48 -7
auth/github/callback.html ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>GitHub Auth Callback</title>
7
+ <script>
8
+ // Process the GitHub callback
9
+ const urlParams = new URLSearchParams(window.location.search);
10
+ const code = urlParams.get('code');
11
+ const error = urlParams.get('error');
12
+
13
+ if (code) {
14
+ // Store the code temporarily (will be exchanged for token in main script)
15
+ localStorage.setItem('githubCode', code);
16
+ window.location.href = '/';
17
+ } else if (error) {
18
+ alert('GitHub authentication failed: ' + error + ' - ' + urlParams.get('error_description'));
19
+ window.location.href = '/';
20
+ }
21
+ </script>
22
+ </head>
23
+ <body>
24
+ <div class="flex items-center justify-center h-screen">
25
+ <div class="text-center">
26
+ <h1 class="text-2xl font-bold mb-4">Processing GitHub Authentication...</h1>
27
+ <div class="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-primary mx-auto"></div>
28
+ </div>
29
+ </div>
30
+ </body>
31
+ </html>
script.js CHANGED
@@ -221,24 +221,65 @@ if (window.location.pathname === '/auth/github/callback') {
221
  const code = new URLSearchParams(window.location.search).get('code');
222
 
223
  if (code) {
224
- // Exchange code for access token
225
- fetch('/api/github-auth', {
 
 
 
 
226
  method: 'POST',
227
  headers: {
 
228
  'Content-Type': 'application/json'
229
  },
230
- body: JSON.stringify({ code })
 
 
 
 
231
  })
232
  .then(response => response.json())
233
  .then(data => {
234
- localStorage.setItem('githubToken', data.access_token);
235
- window.location.href = '/'; // Redirect back to main page
 
 
 
 
236
  })
237
  .catch(error => {
238
  console.error('GitHub auth failed:', error);
239
- alert('GitHub authentication failed');
 
 
 
 
 
 
 
 
 
 
240
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
  }
242
  }
 
243
  function checkConnectionStatus() {
244
- const githubConnected = !document.getElementById('github
 
221
  const code = new URLSearchParams(window.location.search).get('code');
222
 
223
  if (code) {
224
+ // Exchange code for access token (client-side only for demo)
225
+ // Note: In production, you should do this through a backend server
226
+ const clientId = 'YOUR_GITHUB_CLIENT_ID'; // Replace with your actual client ID
227
+ const clientSecret = 'YOUR_GITHUB_CLIENT_SECRET'; // Replace with your actual client secret
228
+
229
+ fetch('https://github.com/login/oauth/access_token', {
230
  method: 'POST',
231
  headers: {
232
+ 'Accept': 'application/json',
233
  'Content-Type': 'application/json'
234
  },
235
+ body: JSON.stringify({
236
+ client_id: clientId,
237
+ client_secret: clientSecret,
238
+ code: code
239
+ })
240
  })
241
  .then(response => response.json())
242
  .then(data => {
243
+ if (data.access_token) {
244
+ localStorage.setItem('githubToken', data.access_token);
245
+ window.location.href = '/'; // Redirect back to main page
246
+ } else {
247
+ throw new Error(data.error_description || 'Failed to get access token');
248
+ }
249
  })
250
  .catch(error => {
251
  console.error('GitHub auth failed:', error);
252
+ alert('GitHub authentication failed: ' + error.message);
253
+ });
254
+ }
255
+ }
256
+ // Simulate loading branches (replace with actual GitHub API call)
257
+ async function simulateLoadBranches(repo) {
258
+ try {
259
+ const response = await fetch(`https://api.github.com/repos/${repo}/branches`, {
260
+ headers: {
261
+ 'Authorization': `token ${localStorage.getItem('githubToken')}`
262
+ }
263
  });
264
+
265
+ if (!response.ok) throw new Error('Failed to fetch branches');
266
+
267
+ const branches = await response.json();
268
+ const select = document.getElementById('branchSelect');
269
+
270
+ select.innerHTML = '<option value="">Select a branch</option>';
271
+ branches.forEach(branch => {
272
+ const option = document.createElement('option');
273
+ option.value = branch.name;
274
+ option.textContent = branch.name;
275
+ select.appendChild(option);
276
+ });
277
+
278
+ select.disabled = false;
279
+ } catch (error) {
280
+ alert('Error loading branches: ' + error.message);
281
  }
282
  }
283
+
284
  function checkConnectionStatus() {
285
+ const githubConnected = !document.getElementById('github