thibaud frere commited on
Commit
9b42726
·
1 Parent(s): 2c44aa2
Files changed (1) hide show
  1. index.html +18 -25
index.html CHANGED
@@ -24,29 +24,28 @@
24
  After clicking "Signin with HF", you will be redirected to this space and the access token + user info will be displayed.
25
  </p>
26
  <img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/sign-in-with-huggingface-xl-dark.svg"
27
- alt="Sign in with Hugging Face" id="login-hf-btn" style="cursor:pointer;max-width:100%;">
28
  <button id="signout" style="display:none;">Sign out</button>
29
  <div id="status"></div>
30
  <pre id="userinfo" style="display:none"></pre>
31
  </div>
32
  <script>
33
- // Utiliser le client_id et client_secret injectés par Hugging Face dans l'environnement du Space
34
  const CLIENT_ID = window.huggingface?.variables?.OAUTH_CLIENT_ID;
35
- const CLIENT_SECRET = window.huggingface?.variables?.OAUTH_CLIENT_SECRET;
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';
39
 
40
  // Helper pour afficher l'état et les infos utilisateur
41
  function showLoggedIn(userinfo) {
42
- document.getElementById('login-hf-btn').style.display = 'none';
43
  document.getElementById('signout').style.display = '';
44
  document.getElementById('status').textContent = 'Logged in!';
45
  document.getElementById('userinfo').style.display = '';
46
  document.getElementById('userinfo').textContent = userinfo;
47
  }
48
  function showLoggedOut() {
49
- document.getElementById('login-hf-btn').style.display = '';
50
  document.getElementById('signout').style.display = 'none';
51
  document.getElementById('status').textContent = '';
52
  document.getElementById('userinfo').style.display = 'none';
@@ -54,21 +53,22 @@
54
  }
55
 
56
  // Sign in button
57
- document.getElementById('login-hf-btn').onclick = function () {
58
  const state = Math.random().toString(36).slice(2);
59
  localStorage.setItem('hf_oauth_state', state);
60
  const url = `${HF_OAUTH_URL}?client_id=${CLIENT_ID}` +
61
  `&redirect_uri=${encodeURIComponent(REDIRECT_URI)}` +
62
- `&response_type=code&scope=openid%20profile&state=${state}`;
63
  window.location = url;
64
  };
65
 
66
  // Sign out button
67
  document.getElementById('signout').onclick = function () {
68
- localStorage.removeItem('hf_oauth_state');
69
  localStorage.removeItem('hf_oauth_token');
70
  localStorage.removeItem('hf_oauth_userinfo');
71
- window.location.href = window.location.pathname;
 
 
72
  };
73
 
74
  // Handle OAuth callback
@@ -91,43 +91,36 @@
91
  document.getElementById('status').textContent = 'Exchanging code for token...';
92
  const resp = await fetch(HF_TOKEN_URL, {
93
  method: 'POST',
94
- headers: {
95
- 'Content-Type': 'application/x-www-form-urlencoded',
96
- 'Authorization': `Basic ${btoa(CLIENT_ID + ':' + CLIENT_SECRET)}`
97
- },
98
  body
99
  });
100
  const data = await resp.json();
101
  if (data.access_token) {
102
  localStorage.setItem('hf_oauth_token', data.access_token);
103
- document.getElementById('status').textContent = 'Logged in!';
104
  // Fetch userinfo
105
  const respUser = await fetch('https://huggingface.co/oauth/userinfo', {
106
  headers: { Authorization: `Bearer ${data.access_token}` }
107
  });
108
  const userinfo = await respUser.json();
109
- localStorage.setItem('hf_oauth_userinfo', JSON.stringify(userinfo, null, 2));
110
- document.getElementById('userinfo').style.display = '';
111
- document.getElementById('userinfo').textContent = JSON.stringify(userinfo, null, 2);
112
- document.getElementById('signout').style.display = '';
113
- document.getElementById('login-hf-btn').style.display = 'none';
114
  // Clean up URL
115
  window.history.replaceState({}, '', window.location.pathname);
116
  } else {
117
  document.getElementById('status').textContent = 'OAuth failed: ' + JSON.stringify(data);
 
118
  }
119
  return;
120
  }
121
 
122
- // If already logged in, show info
123
  const token = localStorage.getItem('hf_oauth_token');
124
  const userinfo = localStorage.getItem('hf_oauth_userinfo');
125
  if (token && userinfo) {
126
- document.getElementById('status').textContent = 'Already logged in!';
127
- document.getElementById('userinfo').style.display = '';
128
- document.getElementById('userinfo').textContent = userinfo;
129
- document.getElementById('signout').style.display = '';
130
- document.getElementById('login-hf-btn').style.display = 'none';
131
  }
132
  };
133
  </script>
 
24
  After clicking "Signin with HF", you will be redirected to this space and the access token + user info will be displayed.
25
  </p>
26
  <img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/sign-in-with-huggingface-xl-dark.svg"
27
+ alt="Sign in with Hugging Face" id="signin" style="cursor:pointer;display:none;max-width:100%;">
28
  <button id="signout" style="display:none;">Sign out</button>
29
  <div id="status"></div>
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';
38
 
39
  // Helper pour afficher l'état et les infos utilisateur
40
  function showLoggedIn(userinfo) {
41
+ document.getElementById('signin').style.display = 'none';
42
  document.getElementById('signout').style.display = '';
43
  document.getElementById('status').textContent = 'Logged in!';
44
  document.getElementById('userinfo').style.display = '';
45
  document.getElementById('userinfo').textContent = userinfo;
46
  }
47
  function showLoggedOut() {
48
+ document.getElementById('signin').style.display = '';
49
  document.getElementById('signout').style.display = 'none';
50
  document.getElementById('status').textContent = '';
51
  document.getElementById('userinfo').style.display = 'none';
 
53
  }
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
 
65
  // Sign out button
66
  document.getElementById('signout').onclick = function () {
 
67
  localStorage.removeItem('hf_oauth_token');
68
  localStorage.removeItem('hf_oauth_userinfo');
69
+ localStorage.removeItem('hf_oauth_state');
70
+ showLoggedOut();
71
+ window.history.replaceState({}, '', window.location.pathname);
72
  };
73
 
74
  // Handle OAuth callback
 
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;
115
  }
116
 
117
+ // Already logged in?
118
  const token = localStorage.getItem('hf_oauth_token');
119
  const userinfo = localStorage.getItem('hf_oauth_userinfo');
120
  if (token && userinfo) {
121
+ showLoggedIn(userinfo);
122
+ } else {
123
+ showLoggedOut();
 
 
124
  }
125
  };
126
  </script>