Spaces:
Runtime error
Runtime error
Charlie
commited on
Commit
·
c63e17f
1
Parent(s):
16c1c4c
update files
Browse files- dist/main.js +53 -25
dist/main.js
CHANGED
|
@@ -241,17 +241,51 @@ async function oauthLoginUrl(opts) {
|
|
| 241 |
}).toString()}`;
|
| 242 |
}
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
// src/main.ts
|
| 245 |
console.log("huggingface env", window.huggingface);
|
| 246 |
-
var oauthResult = null;
|
| 247 |
-
var storedOAuth = localStorage.getItem("oauth");
|
| 248 |
-
if (storedOAuth) {
|
| 249 |
-
try {
|
| 250 |
-
oauthResult = JSON.parse(storedOAuth);
|
| 251 |
-
} catch {
|
| 252 |
-
oauthResult = null;
|
| 253 |
-
}
|
| 254 |
-
}
|
| 255 |
async function getOrganizationInfo(accessToken) {
|
| 256 |
const response = await fetch("https://huggingface.co/api/whoami-v2", {
|
| 257 |
headers: {
|
|
@@ -266,15 +300,19 @@ async function getOrganizationInfo(accessToken) {
|
|
| 266 |
return response.json();
|
| 267 |
}
|
| 268 |
var init = async () => {
|
| 269 |
-
|
| 270 |
-
if (
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
try {
|
| 272 |
-
const orgInfo = await getOrganizationInfo(
|
| 273 |
const preElement = document.querySelector("pre");
|
| 274 |
if (preElement) {
|
| 275 |
preElement.textContent = JSON.stringify(
|
| 276 |
{
|
| 277 |
-
oauth:
|
| 278 |
user: orgInfo
|
| 279 |
},
|
| 280 |
null,
|
|
@@ -284,26 +322,16 @@ var init = async () => {
|
|
| 284 |
} catch (error) {
|
| 285 |
console.error("Failed to fetch organization info:", error);
|
| 286 |
}
|
| 287 |
-
localStorage.setItem("oauth", JSON.stringify(oauthResult));
|
| 288 |
const signoutButton = document.getElementById("signout");
|
| 289 |
if (signoutButton) {
|
| 290 |
signoutButton.style.removeProperty("display");
|
| 291 |
-
signoutButton.onclick =
|
| 292 |
-
localStorage.removeItem("oauth");
|
| 293 |
-
window.location.href = window.location.href.replace(/\?.*$/, "");
|
| 294 |
-
window.location.reload();
|
| 295 |
-
};
|
| 296 |
}
|
| 297 |
} else {
|
| 298 |
const signinButton = document.getElementById("signin");
|
| 299 |
if (signinButton) {
|
| 300 |
signinButton.style.removeProperty("display");
|
| 301 |
-
signinButton.onclick =
|
| 302 |
-
const loginUrl = await oauthLoginUrl({
|
| 303 |
-
scopes: window.huggingface?.variables?.OAUTH_SCOPES ?? ""
|
| 304 |
-
});
|
| 305 |
-
window.location.href = `${loginUrl}&prompt=consent`;
|
| 306 |
-
};
|
| 307 |
}
|
| 308 |
}
|
| 309 |
};
|
|
|
|
| 241 |
}).toString()}`;
|
| 242 |
}
|
| 243 |
|
| 244 |
+
// src/oauth.ts
|
| 245 |
+
var OAuthManager = class {
|
| 246 |
+
constructor() {
|
| 247 |
+
this.oauthResult = null;
|
| 248 |
+
const storedOAuth = localStorage.getItem("oauth");
|
| 249 |
+
if (storedOAuth) {
|
| 250 |
+
try {
|
| 251 |
+
this.oauthResult = JSON.parse(storedOAuth);
|
| 252 |
+
} catch {
|
| 253 |
+
this.oauthResult = null;
|
| 254 |
+
}
|
| 255 |
+
}
|
| 256 |
+
}
|
| 257 |
+
async handleRedirect() {
|
| 258 |
+
this.oauthResult ||= await oauthHandleRedirectIfPresent();
|
| 259 |
+
if (this.oauthResult !== null && this.oauthResult !== false) {
|
| 260 |
+
localStorage.setItem("oauth", JSON.stringify(this.oauthResult));
|
| 261 |
+
}
|
| 262 |
+
return this.oauthResult;
|
| 263 |
+
}
|
| 264 |
+
async initiateLogin() {
|
| 265 |
+
const loginUrl = await oauthLoginUrl({
|
| 266 |
+
scopes: window.huggingface?.variables?.OAUTH_SCOPES ?? ""
|
| 267 |
+
});
|
| 268 |
+
window.location.href = `${loginUrl}&prompt=consent`;
|
| 269 |
+
}
|
| 270 |
+
logout() {
|
| 271 |
+
localStorage.removeItem("oauth");
|
| 272 |
+
window.location.href = window.location.href.replace(/\?.*$/, "");
|
| 273 |
+
window.location.reload();
|
| 274 |
+
}
|
| 275 |
+
getAccessToken() {
|
| 276 |
+
if (this.oauthResult && typeof this.oauthResult === "object") {
|
| 277 |
+
return this.oauthResult.accessToken ?? null;
|
| 278 |
+
}
|
| 279 |
+
return null;
|
| 280 |
+
}
|
| 281 |
+
isAuthenticated() {
|
| 282 |
+
return this.oauthResult !== null && this.oauthResult !== false;
|
| 283 |
+
}
|
| 284 |
+
};
|
| 285 |
+
var oauthManager = new OAuthManager();
|
| 286 |
+
|
| 287 |
// src/main.ts
|
| 288 |
console.log("huggingface env", window.huggingface);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
async function getOrganizationInfo(accessToken) {
|
| 290 |
const response = await fetch("https://huggingface.co/api/whoami-v2", {
|
| 291 |
headers: {
|
|
|
|
| 300 |
return response.json();
|
| 301 |
}
|
| 302 |
var init = async () => {
|
| 303 |
+
await oauthManager.handleRedirect();
|
| 304 |
+
if (oauthManager.isAuthenticated()) {
|
| 305 |
+
const accessToken = oauthManager.getAccessToken();
|
| 306 |
+
if (!accessToken) {
|
| 307 |
+
throw new Error("Access token not found");
|
| 308 |
+
}
|
| 309 |
try {
|
| 310 |
+
const orgInfo = await getOrganizationInfo(accessToken);
|
| 311 |
const preElement = document.querySelector("pre");
|
| 312 |
if (preElement) {
|
| 313 |
preElement.textContent = JSON.stringify(
|
| 314 |
{
|
| 315 |
+
oauth: oauthManager.getAccessToken(),
|
| 316 |
user: orgInfo
|
| 317 |
},
|
| 318 |
null,
|
|
|
|
| 322 |
} catch (error) {
|
| 323 |
console.error("Failed to fetch organization info:", error);
|
| 324 |
}
|
|
|
|
| 325 |
const signoutButton = document.getElementById("signout");
|
| 326 |
if (signoutButton) {
|
| 327 |
signoutButton.style.removeProperty("display");
|
| 328 |
+
signoutButton.onclick = () => oauthManager.logout();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
}
|
| 330 |
} else {
|
| 331 |
const signinButton = document.getElementById("signin");
|
| 332 |
if (signinButton) {
|
| 333 |
signinButton.style.removeProperty("display");
|
| 334 |
+
signinButton.onclick = () => oauthManager.initiateLogin();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
}
|
| 336 |
}
|
| 337 |
};
|