Spaces:
Runtime error
Runtime error
Charlie
commited on
Commit
·
16c1c4c
1
Parent(s):
ee999fe
update files
Browse files- src/main.ts +12 -40
- src/oauth.ts +62 -0
src/main.ts
CHANGED
@@ -1,12 +1,4 @@
|
|
1 |
-
import {
|
2 |
-
|
3 |
-
interface HuggingFaceWindow extends Window {
|
4 |
-
huggingface?: {
|
5 |
-
variables: {
|
6 |
-
OAUTH_SCOPES: string;
|
7 |
-
};
|
8 |
-
};
|
9 |
-
}
|
10 |
|
11 |
interface OrganizationInfo {
|
12 |
type: string;
|
@@ -29,19 +21,6 @@ declare const window: HuggingFaceWindow;
|
|
29 |
|
30 |
console.log("huggingface env", window.huggingface);
|
31 |
|
32 |
-
type OAuthResult = Record<string, any> | null | false;
|
33 |
-
|
34 |
-
let oauthResult: OAuthResult = null;
|
35 |
-
const storedOAuth = localStorage.getItem("oauth");
|
36 |
-
|
37 |
-
if (storedOAuth) {
|
38 |
-
try {
|
39 |
-
oauthResult = JSON.parse(storedOAuth);
|
40 |
-
} catch {
|
41 |
-
oauthResult = null;
|
42 |
-
}
|
43 |
-
}
|
44 |
-
|
45 |
async function getOrganizationInfo(
|
46 |
accessToken: string
|
47 |
): Promise<WhoAmIResponse> {
|
@@ -61,17 +40,22 @@ async function getOrganizationInfo(
|
|
61 |
}
|
62 |
|
63 |
const init = async (): Promise<void> => {
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
if (oauthResult !== null && oauthResult !== false) {
|
67 |
// Get organization info after successful authentication
|
68 |
try {
|
69 |
-
const orgInfo = await getOrganizationInfo(
|
70 |
const preElement = document.querySelector("pre");
|
71 |
if (preElement) {
|
72 |
preElement.textContent = JSON.stringify(
|
73 |
{
|
74 |
-
oauth:
|
75 |
user: orgInfo,
|
76 |
},
|
77 |
null,
|
@@ -82,28 +66,16 @@ const init = async (): Promise<void> => {
|
|
82 |
console.error("Failed to fetch organization info:", error);
|
83 |
}
|
84 |
|
85 |
-
localStorage.setItem("oauth", JSON.stringify(oauthResult));
|
86 |
-
|
87 |
const signoutButton = document.getElementById("signout");
|
88 |
if (signoutButton) {
|
89 |
signoutButton.style.removeProperty("display");
|
90 |
-
signoutButton.onclick =
|
91 |
-
localStorage.removeItem("oauth");
|
92 |
-
window.location.href = window.location.href.replace(/\?.*$/, "");
|
93 |
-
window.location.reload();
|
94 |
-
};
|
95 |
}
|
96 |
} else {
|
97 |
const signinButton = document.getElementById("signin");
|
98 |
if (signinButton) {
|
99 |
signinButton.style.removeProperty("display");
|
100 |
-
signinButton.onclick =
|
101 |
-
// prompt=consent to re-trigger the consent screen instead of silently redirecting
|
102 |
-
const loginUrl = await oauthLoginUrl({
|
103 |
-
scopes: window.huggingface?.variables?.OAUTH_SCOPES ?? "",
|
104 |
-
});
|
105 |
-
window.location.href = `${loginUrl}&prompt=consent`;
|
106 |
-
};
|
107 |
}
|
108 |
}
|
109 |
};
|
|
|
1 |
+
import { oauthManager, HuggingFaceWindow } from "./oauth";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
interface OrganizationInfo {
|
4 |
type: string;
|
|
|
21 |
|
22 |
console.log("huggingface env", window.huggingface);
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
async function getOrganizationInfo(
|
25 |
accessToken: string
|
26 |
): Promise<WhoAmIResponse> {
|
|
|
40 |
}
|
41 |
|
42 |
const init = async (): Promise<void> => {
|
43 |
+
await oauthManager.handleRedirect();
|
44 |
+
|
45 |
+
if (oauthManager.isAuthenticated()) {
|
46 |
+
const accessToken = oauthManager.getAccessToken();
|
47 |
+
if (!accessToken) {
|
48 |
+
throw new Error("Access token not found");
|
49 |
+
}
|
50 |
|
|
|
51 |
// Get organization info after successful authentication
|
52 |
try {
|
53 |
+
const orgInfo = await getOrganizationInfo(accessToken);
|
54 |
const preElement = document.querySelector("pre");
|
55 |
if (preElement) {
|
56 |
preElement.textContent = JSON.stringify(
|
57 |
{
|
58 |
+
oauth: oauthManager.getAccessToken(),
|
59 |
user: orgInfo,
|
60 |
},
|
61 |
null,
|
|
|
66 |
console.error("Failed to fetch organization info:", error);
|
67 |
}
|
68 |
|
|
|
|
|
69 |
const signoutButton = document.getElementById("signout");
|
70 |
if (signoutButton) {
|
71 |
signoutButton.style.removeProperty("display");
|
72 |
+
signoutButton.onclick = () => oauthManager.logout();
|
|
|
|
|
|
|
|
|
73 |
}
|
74 |
} else {
|
75 |
const signinButton = document.getElementById("signin");
|
76 |
if (signinButton) {
|
77 |
signinButton.style.removeProperty("display");
|
78 |
+
signinButton.onclick = () => oauthManager.initiateLogin();
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
}
|
80 |
}
|
81 |
};
|
src/oauth.ts
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { oauthLoginUrl, oauthHandleRedirectIfPresent } from "@huggingface/hub";
|
2 |
+
|
3 |
+
export interface HuggingFaceWindow extends Window {
|
4 |
+
huggingface?: {
|
5 |
+
variables: {
|
6 |
+
OAUTH_SCOPES: string;
|
7 |
+
};
|
8 |
+
};
|
9 |
+
}
|
10 |
+
|
11 |
+
declare const window: HuggingFaceWindow;
|
12 |
+
|
13 |
+
export type OAuthResult = Record<string, any> | null | false;
|
14 |
+
|
15 |
+
export class OAuthManager {
|
16 |
+
private oauthResult: OAuthResult = null;
|
17 |
+
|
18 |
+
constructor() {
|
19 |
+
const storedOAuth = localStorage.getItem("oauth");
|
20 |
+
if (storedOAuth) {
|
21 |
+
try {
|
22 |
+
this.oauthResult = JSON.parse(storedOAuth);
|
23 |
+
} catch {
|
24 |
+
this.oauthResult = null;
|
25 |
+
}
|
26 |
+
}
|
27 |
+
}
|
28 |
+
|
29 |
+
async handleRedirect(): Promise<OAuthResult> {
|
30 |
+
this.oauthResult ||= await oauthHandleRedirectIfPresent();
|
31 |
+
if (this.oauthResult !== null && this.oauthResult !== false) {
|
32 |
+
localStorage.setItem("oauth", JSON.stringify(this.oauthResult));
|
33 |
+
}
|
34 |
+
return this.oauthResult;
|
35 |
+
}
|
36 |
+
|
37 |
+
async initiateLogin(): Promise<void> {
|
38 |
+
const loginUrl = await oauthLoginUrl({
|
39 |
+
scopes: window.huggingface?.variables?.OAUTH_SCOPES ?? "",
|
40 |
+
});
|
41 |
+
window.location.href = `${loginUrl}&prompt=consent`;
|
42 |
+
}
|
43 |
+
|
44 |
+
logout(): void {
|
45 |
+
localStorage.removeItem("oauth");
|
46 |
+
window.location.href = window.location.href.replace(/\?.*$/, "");
|
47 |
+
window.location.reload();
|
48 |
+
}
|
49 |
+
|
50 |
+
getAccessToken(): string | null {
|
51 |
+
if (this.oauthResult && typeof this.oauthResult === "object") {
|
52 |
+
return this.oauthResult.accessToken ?? null;
|
53 |
+
}
|
54 |
+
return null;
|
55 |
+
}
|
56 |
+
|
57 |
+
isAuthenticated(): boolean {
|
58 |
+
return this.oauthResult !== null && this.oauthResult !== false;
|
59 |
+
}
|
60 |
+
}
|
61 |
+
|
62 |
+
export const oauthManager = new OAuthManager();
|