Spaces:
Runtime error
Runtime error
Charlie
commited on
Commit
·
ee999fe
1
Parent(s):
07cf913
update files
Browse files- dist/main.js +28 -3
- src/main.ts +51 -3
dist/main.js
CHANGED
@@ -252,12 +252,37 @@ if (storedOAuth) {
|
|
252 |
oauthResult = null;
|
253 |
}
|
254 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
var init = async () => {
|
256 |
oauthResult ||= await oauthHandleRedirectIfPresent();
|
257 |
if (oauthResult !== null && oauthResult !== false) {
|
258 |
-
|
259 |
-
|
260 |
-
preElement
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
}
|
262 |
localStorage.setItem("oauth", JSON.stringify(oauthResult));
|
263 |
const signoutButton = document.getElementById("signout");
|
|
|
252 |
oauthResult = null;
|
253 |
}
|
254 |
}
|
255 |
+
async function getOrganizationInfo(accessToken) {
|
256 |
+
const response = await fetch("https://huggingface.co/api/whoami-v2", {
|
257 |
+
headers: {
|
258 |
+
Authorization: `Bearer ${accessToken}`
|
259 |
+
}
|
260 |
+
});
|
261 |
+
if (!response.ok) {
|
262 |
+
throw new Error(
|
263 |
+
`Failed to fetch organization info: ${response.statusText}`
|
264 |
+
);
|
265 |
+
}
|
266 |
+
return response.json();
|
267 |
+
}
|
268 |
var init = async () => {
|
269 |
oauthResult ||= await oauthHandleRedirectIfPresent();
|
270 |
if (oauthResult !== null && oauthResult !== false) {
|
271 |
+
try {
|
272 |
+
const orgInfo = await getOrganizationInfo(oauthResult.accessToken);
|
273 |
+
const preElement = document.querySelector("pre");
|
274 |
+
if (preElement) {
|
275 |
+
preElement.textContent = JSON.stringify(
|
276 |
+
{
|
277 |
+
oauth: oauthResult,
|
278 |
+
user: orgInfo
|
279 |
+
},
|
280 |
+
null,
|
281 |
+
2
|
282 |
+
);
|
283 |
+
}
|
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");
|
src/main.ts
CHANGED
@@ -8,6 +8,23 @@ interface HuggingFaceWindow extends Window {
|
|
8 |
};
|
9 |
}
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
declare const window: HuggingFaceWindow;
|
12 |
|
13 |
console.log("huggingface env", window.huggingface);
|
@@ -25,13 +42,44 @@ if (storedOAuth) {
|
|
25 |
}
|
26 |
}
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
const init = async (): Promise<void> => {
|
29 |
oauthResult ||= await oauthHandleRedirectIfPresent();
|
30 |
|
31 |
if (oauthResult !== null && oauthResult !== false) {
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
}
|
36 |
|
37 |
localStorage.setItem("oauth", JSON.stringify(oauthResult));
|
|
|
8 |
};
|
9 |
}
|
10 |
|
11 |
+
interface OrganizationInfo {
|
12 |
+
type: string;
|
13 |
+
id: string;
|
14 |
+
name: string;
|
15 |
+
role: string;
|
16 |
+
}
|
17 |
+
|
18 |
+
interface WhoAmIResponse {
|
19 |
+
type: string;
|
20 |
+
id: string;
|
21 |
+
name: string;
|
22 |
+
email?: string;
|
23 |
+
fullname?: string;
|
24 |
+
avatarUrl?: string;
|
25 |
+
organizations: OrganizationInfo[];
|
26 |
+
}
|
27 |
+
|
28 |
declare const window: HuggingFaceWindow;
|
29 |
|
30 |
console.log("huggingface env", window.huggingface);
|
|
|
42 |
}
|
43 |
}
|
44 |
|
45 |
+
async function getOrganizationInfo(
|
46 |
+
accessToken: string
|
47 |
+
): Promise<WhoAmIResponse> {
|
48 |
+
const response = await fetch("https://huggingface.co/api/whoami-v2", {
|
49 |
+
headers: {
|
50 |
+
Authorization: `Bearer ${accessToken}`,
|
51 |
+
},
|
52 |
+
});
|
53 |
+
|
54 |
+
if (!response.ok) {
|
55 |
+
throw new Error(
|
56 |
+
`Failed to fetch organization info: ${response.statusText}`
|
57 |
+
);
|
58 |
+
}
|
59 |
+
|
60 |
+
return response.json();
|
61 |
+
}
|
62 |
+
|
63 |
const init = async (): Promise<void> => {
|
64 |
oauthResult ||= await oauthHandleRedirectIfPresent();
|
65 |
|
66 |
if (oauthResult !== null && oauthResult !== false) {
|
67 |
+
// Get organization info after successful authentication
|
68 |
+
try {
|
69 |
+
const orgInfo = await getOrganizationInfo(oauthResult.accessToken);
|
70 |
+
const preElement = document.querySelector("pre");
|
71 |
+
if (preElement) {
|
72 |
+
preElement.textContent = JSON.stringify(
|
73 |
+
{
|
74 |
+
oauth: oauthResult,
|
75 |
+
user: orgInfo,
|
76 |
+
},
|
77 |
+
null,
|
78 |
+
2
|
79 |
+
);
|
80 |
+
}
|
81 |
+
} catch (error) {
|
82 |
+
console.error("Failed to fetch organization info:", error);
|
83 |
}
|
84 |
|
85 |
localStorage.setItem("oauth", JSON.stringify(oauthResult));
|