Spaces:
Runtime error
Runtime error
Commit
·
cacd266
1
Parent(s):
46971ee
pls
Browse files- README.md +6 -0
- src/components/sign-in-with-hf-button.tsx +17 -14
README.md
CHANGED
|
@@ -5,4 +5,10 @@ colorFrom: purple
|
|
| 5 |
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
app_port: 7860
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
|
|
|
| 5 |
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
app_port: 7860
|
| 8 |
+
hf_oauth: true
|
| 9 |
+
hf_oauth_scopes:
|
| 10 |
+
- openid
|
| 11 |
+
- profile
|
| 12 |
+
- inference-api
|
| 13 |
+
- email
|
| 14 |
---
|
src/components/sign-in-with-hf-button.tsx
CHANGED
|
@@ -12,10 +12,7 @@ const SSO_URL = `https://huggingface.co/oauth/authorize?client_id=${CLIENT_ID}&r
|
|
| 12 |
const OAUTH_API_BASE = "https://huggingface.co/oauth/token";
|
| 13 |
const CLIENT_SECRET = import.meta.env.VITE_HUGGINGFACE_CLIENT_SECRET; // THIS IS UNSAFE, must fix before real deploy
|
| 14 |
|
| 15 |
-
import {
|
| 16 |
-
oauthLoginUrl,
|
| 17 |
-
oauthHandleRedirectIfPresent,
|
| 18 |
-
} from "@huggingface/hub";
|
| 19 |
|
| 20 |
export const SignInWithHuggingFaceButton = () => {
|
| 21 |
const [isSignedIn, setIsSignedIn] = useState(false);
|
|
@@ -25,7 +22,7 @@ export const SignInWithHuggingFaceButton = () => {
|
|
| 25 |
// useEffect(() => {
|
| 26 |
// const idToken = window.localStorage.getItem("huggingface_id_token");
|
| 27 |
// const accessToken = window.localStorage.getItem("huggingface_access_token");
|
| 28 |
-
|
| 29 |
// if (idToken && accessToken) {
|
| 30 |
// const idTokenObject = JSON.parse(idToken);
|
| 31 |
// if (idTokenObject.exp > Date.now() / 1000) {
|
|
@@ -36,7 +33,6 @@ export const SignInWithHuggingFaceButton = () => {
|
|
| 36 |
// }
|
| 37 |
// }
|
| 38 |
|
| 39 |
-
|
| 40 |
// async function fetchToken() {
|
| 41 |
// const code = new URLSearchParams(window.location.search).get("code");
|
| 42 |
// if (code) {
|
|
@@ -75,20 +71,27 @@ export const SignInWithHuggingFaceButton = () => {
|
|
| 75 |
useEffect(() => {
|
| 76 |
async function fetchToken() {
|
| 77 |
console.log("fetching token", window.location.href);
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
}
|
| 88 |
|
| 89 |
fetchToken();
|
| 90 |
}, []);
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if (isLoading) {
|
| 93 |
return <div>Loading...</div>;
|
| 94 |
}
|
|
@@ -98,7 +101,7 @@ export const SignInWithHuggingFaceButton = () => {
|
|
| 98 |
}
|
| 99 |
|
| 100 |
return (
|
| 101 |
-
<a
|
| 102 |
<img
|
| 103 |
src="https://huggingface.co/datasets/huggingface/badges/resolve/main/sign-in-with-huggingface-xl.svg"
|
| 104 |
alt="Sign in with Hugging Face"
|
|
|
|
| 12 |
const OAUTH_API_BASE = "https://huggingface.co/oauth/token";
|
| 13 |
const CLIENT_SECRET = import.meta.env.VITE_HUGGINGFACE_CLIENT_SECRET; // THIS IS UNSAFE, must fix before real deploy
|
| 14 |
|
| 15 |
+
import { oauthLoginUrl, oauthHandleRedirectIfPresent } from "@huggingface/hub";
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
export const SignInWithHuggingFaceButton = () => {
|
| 18 |
const [isSignedIn, setIsSignedIn] = useState(false);
|
|
|
|
| 22 |
// useEffect(() => {
|
| 23 |
// const idToken = window.localStorage.getItem("huggingface_id_token");
|
| 24 |
// const accessToken = window.localStorage.getItem("huggingface_access_token");
|
| 25 |
+
|
| 26 |
// if (idToken && accessToken) {
|
| 27 |
// const idTokenObject = JSON.parse(idToken);
|
| 28 |
// if (idTokenObject.exp > Date.now() / 1000) {
|
|
|
|
| 33 |
// }
|
| 34 |
// }
|
| 35 |
|
|
|
|
| 36 |
// async function fetchToken() {
|
| 37 |
// const code = new URLSearchParams(window.location.search).get("code");
|
| 38 |
// if (code) {
|
|
|
|
| 71 |
useEffect(() => {
|
| 72 |
async function fetchToken() {
|
| 73 |
console.log("fetching token", window.location.href);
|
| 74 |
+
const oauthResult = await oauthHandleRedirectIfPresent();
|
| 75 |
|
| 76 |
+
if (!oauthResult) {
|
| 77 |
+
// If the user is not logged in, redirect to the login page
|
| 78 |
+
window.location.href = await oauthLoginUrl();
|
| 79 |
+
}
|
| 80 |
|
| 81 |
+
// You can use oauthResult.accessToken, oauthResult.accessTokenExpiresAt and oauthResult.userInfo
|
| 82 |
+
console.log(oauthResult);
|
| 83 |
}
|
| 84 |
|
| 85 |
fetchToken();
|
| 86 |
}, []);
|
| 87 |
|
| 88 |
+
const handleLogin = async () => {
|
| 89 |
+
window.location.href =
|
| 90 |
+
(await oauthLoginUrl({
|
| 91 |
+
scopes: window.huggingface.variables.OAUTH_SCOPES,
|
| 92 |
+
})) + "&prompt=consent";
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
if (isLoading) {
|
| 96 |
return <div>Loading...</div>;
|
| 97 |
}
|
|
|
|
| 101 |
}
|
| 102 |
|
| 103 |
return (
|
| 104 |
+
<a onClick={handleLogin} href="#" rel="nofollow">
|
| 105 |
<img
|
| 106 |
src="https://huggingface.co/datasets/huggingface/badges/resolve/main/sign-in-with-huggingface-xl.svg"
|
| 107 |
alt="Sign in with Hugging Face"
|