thibaud frere commited on
Commit
ee3250c
·
1 Parent(s): 9b42726
Files changed (1) hide show
  1. index.html +50 -21
index.html CHANGED
@@ -30,8 +30,9 @@
30
  <pre id="userinfo" style="display:none"></pre>
31
  </div>
32
  <script>
33
- // Utiliser le client_id injecté par Hugging Face dans l'environnement du Space
34
- const CLIENT_ID = window.huggingface?.variables?.OAUTH_CLIENT_ID;
 
35
  const REDIRECT_URI = window.location.origin + window.location.pathname;
36
  const HF_OAUTH_URL = 'https://huggingface.co/oauth/authorize';
37
  const HF_TOKEN_URL = 'https://huggingface.co/oauth/token';
@@ -54,11 +55,17 @@
54
 
55
  // Sign in button
56
  document.getElementById('signin').onclick = function () {
 
 
 
 
 
57
  const state = Math.random().toString(36).slice(2);
58
  localStorage.setItem('hf_oauth_state', state);
59
  const url = `${HF_OAUTH_URL}?client_id=${CLIENT_ID}` +
60
  `&redirect_uri=${encodeURIComponent(REDIRECT_URI)}` +
61
  `&response_type=code&scope=openid%20profile&state=${state}&prompt=consent`;
 
62
  window.location = url;
63
  };
64
 
@@ -73,6 +80,9 @@
73
 
74
  // Handle OAuth callback
75
  window.onload = async function () {
 
 
 
76
  // If returning from OAuth redirect
77
  const params = new URLSearchParams(window.location.search);
78
  if (params.has('code') && params.has('state')) {
@@ -81,6 +91,12 @@
81
  document.getElementById('status').textContent = 'Invalid state, possible CSRF detected.';
82
  return;
83
  }
 
 
 
 
 
 
84
  const code = params.get('code');
85
  const body = new URLSearchParams({
86
  client_id: CLIENT_ID,
@@ -88,27 +104,40 @@
88
  code: code,
89
  redirect_uri: REDIRECT_URI
90
  });
 
 
91
  document.getElementById('status').textContent = 'Exchanging code for token...';
92
- const resp = await fetch(HF_TOKEN_URL, {
93
- method: 'POST',
94
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
95
- body
96
- });
97
- const data = await resp.json();
98
- if (data.access_token) {
99
- localStorage.setItem('hf_oauth_token', data.access_token);
100
- // Fetch userinfo
101
- const respUser = await fetch('https://huggingface.co/oauth/userinfo', {
102
- headers: { Authorization: `Bearer ${data.access_token}` }
103
  });
104
- const userinfo = await respUser.json();
105
- const userinfoStr = JSON.stringify(userinfo, null, 2);
106
- localStorage.setItem('hf_oauth_userinfo', userinfoStr);
107
- showLoggedIn(userinfoStr);
108
- // Clean up URL
109
- window.history.replaceState({}, '', window.location.pathname);
110
- } else {
111
- document.getElementById('status').textContent = 'OAuth failed: ' + JSON.stringify(data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  showLoggedOut();
113
  }
114
  return;
 
30
  <pre id="userinfo" style="display:none"></pre>
31
  </div>
32
  <script>
33
+ // Le client_id est automatiquement injecté par Hugging Face dans les Spaces avec hf_oauth: true
34
+ // Il est disponible via window.huggingfaceConfig ou directement depuis l'URL de callback
35
+ const CLIENT_ID = window.huggingfaceConfig?.oauth?.client_id || 'e0906ea6-519f-4aa0-a128-9b7df044e477';
36
  const REDIRECT_URI = window.location.origin + window.location.pathname;
37
  const HF_OAUTH_URL = 'https://huggingface.co/oauth/authorize';
38
  const HF_TOKEN_URL = 'https://huggingface.co/oauth/token';
 
55
 
56
  // Sign in button
57
  document.getElementById('signin').onclick = function () {
58
+ if (!CLIENT_ID) {
59
+ document.getElementById('status').textContent = 'Erreur: CLIENT_ID non défini. Vérifiez la configuration OAuth du Space.';
60
+ return;
61
+ }
62
+
63
  const state = Math.random().toString(36).slice(2);
64
  localStorage.setItem('hf_oauth_state', state);
65
  const url = `${HF_OAUTH_URL}?client_id=${CLIENT_ID}` +
66
  `&redirect_uri=${encodeURIComponent(REDIRECT_URI)}` +
67
  `&response_type=code&scope=openid%20profile&state=${state}&prompt=consent`;
68
+ console.log('Redirecting to:', url);
69
  window.location = url;
70
  };
71
 
 
80
 
81
  // Handle OAuth callback
82
  window.onload = async function () {
83
+ console.log('CLIENT_ID:', CLIENT_ID);
84
+ console.log('REDIRECT_URI:', REDIRECT_URI);
85
+
86
  // If returning from OAuth redirect
87
  const params = new URLSearchParams(window.location.search);
88
  if (params.has('code') && params.has('state')) {
 
91
  document.getElementById('status').textContent = 'Invalid state, possible CSRF detected.';
92
  return;
93
  }
94
+
95
+ if (!CLIENT_ID) {
96
+ document.getElementById('status').textContent = 'Erreur: CLIENT_ID non défini. Vérifiez la configuration OAuth du Space.';
97
+ return;
98
+ }
99
+
100
  const code = params.get('code');
101
  const body = new URLSearchParams({
102
  client_id: CLIENT_ID,
 
104
  code: code,
105
  redirect_uri: REDIRECT_URI
106
  });
107
+
108
+ console.log('Token exchange body:', body.toString());
109
  document.getElementById('status').textContent = 'Exchanging code for token...';
110
+
111
+ try {
112
+ const resp = await fetch(HF_TOKEN_URL, {
113
+ method: 'POST',
114
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
115
+ body
 
 
 
 
 
116
  });
117
+
118
+ console.log('Token response status:', resp.status);
119
+ const data = await resp.json();
120
+ console.log('Token response data:', data);
121
+
122
+ if (data.access_token) {
123
+ localStorage.setItem('hf_oauth_token', data.access_token);
124
+ // Fetch userinfo
125
+ const respUser = await fetch('https://huggingface.co/oauth/userinfo', {
126
+ headers: { Authorization: `Bearer ${data.access_token}` }
127
+ });
128
+ const userinfo = await respUser.json();
129
+ const userinfoStr = JSON.stringify(userinfo, null, 2);
130
+ localStorage.setItem('hf_oauth_userinfo', userinfoStr);
131
+ showLoggedIn(userinfoStr);
132
+ // Clean up URL
133
+ window.history.replaceState({}, '', window.location.pathname);
134
+ } else {
135
+ document.getElementById('status').textContent = 'OAuth failed: ' + JSON.stringify(data);
136
+ showLoggedOut();
137
+ }
138
+ } catch (error) {
139
+ console.error('Erreur lors de l\'échange du token:', error);
140
+ document.getElementById('status').textContent = 'Erreur réseau: ' + error.message;
141
  showLoggedOut();
142
  }
143
  return;