Spaces:
Runtime error
Runtime error
Charlie
commited on
Commit
·
5ecbfe5
1
Parent(s):
67e3349
update files
Browse files- dist/index.js +72 -3
- dist/index.js.map +1 -0
- index.html +14 -0
- package-lock.json +62 -0
- package.json +4 -0
- src/index.ts +82 -3
- tsconfig.json +9 -2
dist/index.js
CHANGED
@@ -1,4 +1,73 @@
|
|
1 |
-
function
|
2 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
}
|
4 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2 |
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3 |
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4 |
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 |
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 |
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7 |
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 |
+
});
|
9 |
+
};
|
10 |
+
import { oauthLoginUrl, oauthHandleRedirectIfPresent } from "@huggingface/hub";
|
11 |
+
function initializeAuth() {
|
12 |
+
return __awaiter(this, void 0, void 0, function* () {
|
13 |
+
try {
|
14 |
+
console.log("huggingface env", window.huggingface);
|
15 |
+
let oauthResult = null;
|
16 |
+
const storedAuth = localStorage.getItem("oauth");
|
17 |
+
if (storedAuth) {
|
18 |
+
try {
|
19 |
+
oauthResult = JSON.parse(storedAuth);
|
20 |
+
}
|
21 |
+
catch (error) {
|
22 |
+
console.error("Failed to parse stored OAuth data:", error);
|
23 |
+
localStorage.removeItem("oauth");
|
24 |
+
}
|
25 |
+
}
|
26 |
+
const redirectResult = yield oauthHandleRedirectIfPresent();
|
27 |
+
if (redirectResult) {
|
28 |
+
oauthResult = redirectResult;
|
29 |
+
}
|
30 |
+
if (oauthResult) {
|
31 |
+
const preElement = document.querySelector("pre");
|
32 |
+
if (preElement) {
|
33 |
+
preElement.textContent = JSON.stringify(oauthResult, null, 2);
|
34 |
+
}
|
35 |
+
localStorage.setItem("oauth", JSON.stringify(oauthResult));
|
36 |
+
const signoutButton = document.getElementById("signout");
|
37 |
+
if (signoutButton) {
|
38 |
+
signoutButton.style.removeProperty("display");
|
39 |
+
signoutButton.onclick = () => __awaiter(this, void 0, void 0, function* () {
|
40 |
+
localStorage.removeItem("oauth");
|
41 |
+
window.location.href = window.location.href.replace(/\?.*$/, "");
|
42 |
+
window.location.reload();
|
43 |
+
});
|
44 |
+
}
|
45 |
+
}
|
46 |
+
else {
|
47 |
+
const signinButton = document.getElementById("signin");
|
48 |
+
if (signinButton) {
|
49 |
+
signinButton.style.removeProperty("display");
|
50 |
+
signinButton.onclick = () => __awaiter(this, void 0, void 0, function* () {
|
51 |
+
try {
|
52 |
+
const loginUrl = yield oauthLoginUrl({
|
53 |
+
scopes: window.huggingface.variables.OAUTH_SCOPES,
|
54 |
+
});
|
55 |
+
window.location.href = `${loginUrl}&prompt=consent`;
|
56 |
+
}
|
57 |
+
catch (error) {
|
58 |
+
console.error("Failed to generate login URL:", error);
|
59 |
+
}
|
60 |
+
});
|
61 |
+
}
|
62 |
+
}
|
63 |
+
}
|
64 |
+
catch (error) {
|
65 |
+
console.error("Authentication initialization failed:", error);
|
66 |
+
}
|
67 |
+
});
|
68 |
}
|
69 |
+
// Initialize the authentication flow
|
70 |
+
initializeAuth().catch((error) => {
|
71 |
+
console.error("Fatal error during authentication initialization:", error);
|
72 |
+
});
|
73 |
+
//# sourceMappingURL=index.js.map
|
dist/index.js.map
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,4BAA4B,EAAE,MAAM,kBAAkB,CAAC;AAqB/E,SAAe,cAAc;;QAC3B,IAAI,CAAC;YACH,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;YAEnD,IAAI,WAAW,GAAuB,IAAI,CAAC;YAC3C,MAAM,UAAU,GAAG,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YAEjD,IAAI,UAAU,EAAE,CAAC;gBACf,IAAI,CAAC;oBACH,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;gBACvC,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,OAAO,CAAC,KAAK,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAC;oBAC3D,YAAY,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;gBACnC,CAAC;YACH,CAAC;YAED,MAAM,cAAc,GAAG,MAAM,4BAA4B,EAAE,CAAC;YAC5D,IAAI,cAAc,EAAE,CAAC;gBACnB,WAAW,GAAG,cAA6B,CAAC;YAC9C,CAAC;YAED,IAAI,WAAW,EAAE,CAAC;gBAChB,MAAM,UAAU,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;gBACjD,IAAI,UAAU,EAAE,CAAC;oBACf,UAAU,CAAC,WAAW,GAAG,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;gBAChE,CAAC;gBAED,YAAY,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC;gBAE3D,MAAM,aAAa,G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AK,CAAC,CAAC,KAAK,EAAE,EAAE;IAC/B,OAAO,CAAC,KAAK,CAAC,mDAAmD,EAAE,KAAK,CAAC,CAAC;AAC5E,CAAC,CAAC,CAAC"}
|
index.html
CHANGED
@@ -2,6 +2,20 @@
|
|
2 |
<html>
|
3 |
|
4 |
<body>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
<script type="module" src="dist/index.js"></script>
|
6 |
</body>
|
7 |
|
|
|
2 |
<html>
|
3 |
|
4 |
<body>
|
5 |
+
<div class="card" style="margin-bottom: 2rem;">
|
6 |
+
<h1>OAuth in a static Space</h1>
|
7 |
+
<p>Checkout the <a href="https://huggingface.co/spaces/huggingfacejs/client-side-oauth/blob/main/index.html"
|
8 |
+
target="_blank">index.html</a> file to see the few lines of code
|
9 |
+
enabling this space</p>
|
10 |
+
<p>After clicking "Signin with HF", you will be redirected to this space and the access token + user info will
|
11 |
+
be displayed.</p>
|
12 |
+
</div>
|
13 |
+
<img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/sign-in-with-huggingface-xl-dark.svg"
|
14 |
+
alt="Sign in with Hugging Face" style="cursor: pointer; display: none;" id="signin">
|
15 |
+
<button id="signout" style="display: none">Sign out</button>
|
16 |
+
<pre>
|
17 |
+
|
18 |
+
</pre>
|
19 |
<script type="module" src="dist/index.js"></script>
|
20 |
</body>
|
21 |
|
package-lock.json
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "apidemo",
|
3 |
+
"version": "1.0.0",
|
4 |
+
"lockfileVersion": 3,
|
5 |
+
"requires": true,
|
6 |
+
"packages": {
|
7 |
+
"": {
|
8 |
+
"name": "apidemo",
|
9 |
+
"version": "1.0.0",
|
10 |
+
"dependencies": {
|
11 |
+
"@huggingface/hub": "^1.0.1",
|
12 |
+
"typescript": "^5.7.3"
|
13 |
+
},
|
14 |
+
"devDependencies": {
|
15 |
+
"@types/node": "^22.13.1"
|
16 |
+
}
|
17 |
+
},
|
18 |
+
"node_modules/@huggingface/hub": {
|
19 |
+
"version": "1.0.1",
|
20 |
+
"resolved": "https://registry.npmjs.org/@huggingface/hub/-/hub-1.0.1.tgz",
|
21 |
+
"integrity": "sha512-wogGVETaNUV/wYBkny0uQD48L0rK9cttVtbaA1Rw/pGCuSYoZ8YlvTV6zymsGJfXaxQU8zup0aOR2XLIf6HVfg==",
|
22 |
+
"dependencies": {
|
23 |
+
"@huggingface/tasks": "^0.15.9"
|
24 |
+
},
|
25 |
+
"engines": {
|
26 |
+
"node": ">=18"
|
27 |
+
}
|
28 |
+
},
|
29 |
+
"node_modules/@huggingface/tasks": {
|
30 |
+
"version": "0.15.9",
|
31 |
+
"resolved": "https://registry.npmjs.org/@huggingface/tasks/-/tasks-0.15.9.tgz",
|
32 |
+
"integrity": "sha512-cbnZcpMHKdhURWIplVP4obHxAZcxjyRm0zI7peTPksZN4CtIOMmJC4ZqGEymo0lk+0VNkXD7ULwFJ3JjT/VpkQ=="
|
33 |
+
},
|
34 |
+
"node_modules/@types/node": {
|
35 |
+
"version": "22.13.1",
|
36 |
+
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.1.tgz",
|
37 |
+
"integrity": "sha512-jK8uzQlrvXqEU91UxiK5J7pKHyzgnI1Qnl0QDHIgVGuolJhRb9EEl28Cj9b3rGR8B2lhFCtvIm5os8lFnO/1Ew==",
|
38 |
+
"dev": true,
|
39 |
+
"dependencies": {
|
40 |
+
"undici-types": "~6.20.0"
|
41 |
+
}
|
42 |
+
},
|
43 |
+
"node_modules/typescript": {
|
44 |
+
"version": "5.7.3",
|
45 |
+
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.7.3.tgz",
|
46 |
+
"integrity": "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw==",
|
47 |
+
"bin": {
|
48 |
+
"tsc": "bin/tsc",
|
49 |
+
"tsserver": "bin/tsserver"
|
50 |
+
},
|
51 |
+
"engines": {
|
52 |
+
"node": ">=14.17"
|
53 |
+
}
|
54 |
+
},
|
55 |
+
"node_modules/undici-types": {
|
56 |
+
"version": "6.20.0",
|
57 |
+
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.20.0.tgz",
|
58 |
+
"integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==",
|
59 |
+
"dev": true
|
60 |
+
}
|
61 |
+
}
|
62 |
+
}
|
package.json
CHANGED
@@ -3,10 +3,14 @@
|
|
3 |
"version": "1.0.0",
|
4 |
"main": "index.js",
|
5 |
"dependencies": {
|
|
|
6 |
"typescript": "^5.7.3"
|
7 |
},
|
8 |
"scripts": {
|
9 |
"build": "tsc",
|
10 |
"start": "node dist/index.js"
|
|
|
|
|
|
|
11 |
}
|
12 |
}
|
|
|
3 |
"version": "1.0.0",
|
4 |
"main": "index.js",
|
5 |
"dependencies": {
|
6 |
+
"@huggingface/hub": "^1.0.1",
|
7 |
"typescript": "^5.7.3"
|
8 |
},
|
9 |
"scripts": {
|
10 |
"build": "tsc",
|
11 |
"start": "node dist/index.js"
|
12 |
+
},
|
13 |
+
"devDependencies": {
|
14 |
+
"@types/node": "^22.13.1"
|
15 |
}
|
16 |
}
|
src/index.ts
CHANGED
@@ -1,5 +1,84 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
}
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
1 |
+
import { oauthLoginUrl, oauthHandleRedirectIfPresent } from "@huggingface/hub";
|
2 |
+
|
3 |
+
interface OAuthResult {
|
4 |
+
accessToken: string;
|
5 |
+
userInfo?: {
|
6 |
+
name?: string;
|
7 |
+
email?: string;
|
8 |
+
[key: string]: any;
|
9 |
+
};
|
10 |
+
}
|
11 |
+
|
12 |
+
declare global {
|
13 |
+
interface Window {
|
14 |
+
huggingface: {
|
15 |
+
variables: {
|
16 |
+
OAUTH_SCOPES: string; // Changed from string[] to string
|
17 |
+
};
|
18 |
+
};
|
19 |
+
}
|
20 |
+
}
|
21 |
+
|
22 |
+
async function initializeAuth(): Promise<void> {
|
23 |
+
try {
|
24 |
+
console.log("huggingface env", window.huggingface);
|
25 |
+
|
26 |
+
let oauthResult: OAuthResult | null = null;
|
27 |
+
const storedAuth = localStorage.getItem("oauth");
|
28 |
+
|
29 |
+
if (storedAuth) {
|
30 |
+
try {
|
31 |
+
oauthResult = JSON.parse(storedAuth);
|
32 |
+
} catch (error) {
|
33 |
+
console.error("Failed to parse stored OAuth data:", error);
|
34 |
+
localStorage.removeItem("oauth");
|
35 |
+
}
|
36 |
+
}
|
37 |
+
|
38 |
+
const redirectResult = await oauthHandleRedirectIfPresent();
|
39 |
+
if (redirectResult) {
|
40 |
+
oauthResult = redirectResult as OAuthResult;
|
41 |
+
}
|
42 |
+
|
43 |
+
if (oauthResult) {
|
44 |
+
const preElement = document.querySelector("pre");
|
45 |
+
if (preElement) {
|
46 |
+
preElement.textContent = JSON.stringify(oauthResult, null, 2);
|
47 |
+
}
|
48 |
+
|
49 |
+
localStorage.setItem("oauth", JSON.stringify(oauthResult));
|
50 |
+
|
51 |
+
const signoutButton = document.getElementById("signout");
|
52 |
+
if (signoutButton) {
|
53 |
+
signoutButton.style.removeProperty("display");
|
54 |
+
signoutButton.onclick = async () => {
|
55 |
+
localStorage.removeItem("oauth");
|
56 |
+
window.location.href = window.location.href.replace(/\?.*$/, "");
|
57 |
+
window.location.reload();
|
58 |
+
};
|
59 |
+
}
|
60 |
+
} else {
|
61 |
+
const signinButton = document.getElementById("signin");
|
62 |
+
if (signinButton) {
|
63 |
+
signinButton.style.removeProperty("display");
|
64 |
+
signinButton.onclick = async () => {
|
65 |
+
try {
|
66 |
+
const loginUrl = await oauthLoginUrl({
|
67 |
+
scopes: window.huggingface.variables.OAUTH_SCOPES,
|
68 |
+
});
|
69 |
+
window.location.href = `${loginUrl}&prompt=consent`;
|
70 |
+
} catch (error) {
|
71 |
+
console.error("Failed to generate login URL:", error);
|
72 |
+
}
|
73 |
+
};
|
74 |
+
}
|
75 |
+
}
|
76 |
+
} catch (error) {
|
77 |
+
console.error("Authentication initialization failed:", error);
|
78 |
+
}
|
79 |
}
|
80 |
|
81 |
+
// Initialize the authentication flow
|
82 |
+
initializeAuth().catch((error) => {
|
83 |
+
console.error("Fatal error during authentication initialization:", error);
|
84 |
+
});
|
tsconfig.json
CHANGED
@@ -2,6 +2,13 @@
|
|
2 |
"compilerOptions": {
|
3 |
"outDir": "./dist",
|
4 |
"module": "es6",
|
5 |
-
"target": "es6"
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
}
|
|
|
2 |
"compilerOptions": {
|
3 |
"outDir": "./dist",
|
4 |
"module": "es6",
|
5 |
+
"target": "es6",
|
6 |
+
"moduleResolution": "node",
|
7 |
+
"allowJs": true,
|
8 |
+
"esModuleInterop": true,
|
9 |
+
"strict": true,
|
10 |
+
"lib": ["es2020", "dom"],
|
11 |
+
"sourceMap": true
|
12 |
+
},
|
13 |
+
"include": ["src/**/*"]
|
14 |
}
|