Vaibhavs10 commited on
Commit
a13ea2e
·
1 Parent(s): bbc42e5
Files changed (1) hide show
  1. index.html +34 -33
index.html CHANGED
@@ -106,7 +106,8 @@
106
  </div>
107
  </div>
108
 
109
- <script>
 
110
  // DOM Elements
111
  const loginBtn = document.getElementById('login-btn');
112
  const logoutBtn = document.getElementById('logout-btn');
@@ -128,8 +129,9 @@
128
  let messageCount = 0;
129
 
130
  // Initialize
131
- document.addEventListener('DOMContentLoaded', () => {
132
- checkAuth();
 
133
  messageInput.disabled = false; // Always enable input
134
  });
135
 
@@ -182,37 +184,12 @@
182
 
183
  async function handleLogin() {
184
  try {
185
- const token = prompt("Enter your Hugging Face API token (get it from https://huggingface.co/settings/tokens):");
186
- if (!token) return;
187
-
188
- // Test the token with a lightweight request
189
- const testClient = new hf.Inference(token);
190
- const response = await fetch('https://huggingface.co/api/whoami-v2', {
191
- headers: {
192
- 'Authorization': `Bearer ${token}`
193
- }
194
- });
195
-
196
- if (!response.ok) {
197
- throw new Error('Invalid token');
198
- }
199
-
200
- const userData = await response.json();
201
- const user = {
202
- name: userData.name || 'Hugging Face User',
203
- avatar: userData.avatarUrl || 'https://huggingface.co/front/assets/huggingface_logo-noborder.svg'
204
- };
205
-
206
- localStorage.setItem('hf_token', token);
207
- localStorage.setItem('hf_user', JSON.stringify(user));
208
- currentUser = user;
209
- hfClient = testClient;
210
- setupAuthenticatedUI();
211
-
212
- addMessage('assistant', `Hello ${user.name}! How can I help you today?`);
213
  } catch (error) {
214
- console.error("Login error:", error);
215
- alert(`Login failed: ${error.message}. Please check your token and try again.`);
216
  }
217
  }
218
 
@@ -370,6 +347,30 @@
370
  return messages;
371
  }
372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  </script>
374
  <p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=reach-vb/hugging-chat" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
375
  </html>
 
106
  </div>
107
  </div>
108
 
109
+ <script type="module">
110
+ import { oauthLoginUrl, oauthHandleRedirectIfPresent } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm';
111
  // DOM Elements
112
  const loginBtn = document.getElementById('login-btn');
113
  const logoutBtn = document.getElementById('logout-btn');
 
129
  let messageCount = 0;
130
 
131
  // Initialize
132
+ document.addEventListener('DOMContentLoaded', async () => {
133
+ await processRedirect();
134
+ await checkAuth();
135
  messageInput.disabled = false; // Always enable input
136
  });
137
 
 
184
 
185
  async function handleLogin() {
186
  try {
187
+ // Redirect user to Hugging Face OAuth page
188
+ const url = await oauthLoginUrl();
189
+ window.location.href = url;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  } catch (error) {
191
+ console.error("Login redirect error:", error);
192
+ alert(`Unable to redirect to Hugging Face login: ${error.message}`);
193
  }
194
  }
195
 
 
347
  return messages;
348
  }
349
 
350
+ // Handle OAuth redirect result
351
+ async function processRedirect() {
352
+ try {
353
+ const oauthResult = await oauthHandleRedirectIfPresent();
354
+ if (oauthResult) {
355
+ const token = oauthResult.accessToken;
356
+ const info = oauthResult.userInfo || {};
357
+ const user = {
358
+ name: info.name || info.preferred_username || 'Hugging Face User',
359
+ avatar: info.picture || 'https://huggingface.co/front/assets/huggingface_logo-noborder.svg'
360
+ };
361
+ localStorage.setItem('hf_token', token);
362
+ localStorage.setItem('hf_user', JSON.stringify(user));
363
+ currentUser = user;
364
+ hfClient = new hf.Inference(token);
365
+ setupAuthenticatedUI();
366
+ if (messageCount === 0) {
367
+ addMessage('assistant', `Hello ${user.name}! How can I help you today?`);
368
+ }
369
+ }
370
+ } catch (err) {
371
+ console.error('OAuth processing failed:', err);
372
+ }
373
+ }
374
  </script>
375
  <p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=reach-vb/hugging-chat" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
376
  </html>