stillerman commited on
Commit
46971ee
·
1 Parent(s): 44acf32

space auth?

Browse files
src/components/sign-in-with-hf-button.tsx CHANGED
@@ -5,62 +5,85 @@ import { jwtDecode } from "jwt-decode";
5
 
6
  const CLIENT_ID = "a67ef241-fb7e-4300-a6bd-8430a7565c9a";
7
  const REDIRECT_URI = "https://huggingfacetb-wikispeedia.hf.space";
 
8
  const SCOPE = "openid%20profile%20email%20inference-api";
9
  const STATE = "1234567890";
10
  const SSO_URL = `https://huggingface.co/oauth/authorize?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=${SCOPE}&prompt=consent&state=${STATE}`;
11
  const OAUTH_API_BASE = "https://huggingface.co/oauth/token";
12
  const CLIENT_SECRET = import.meta.env.VITE_HUGGINGFACE_CLIENT_SECRET; // THIS IS UNSAFE, must fix before real deploy
13
 
 
 
 
 
 
14
  export const SignInWithHuggingFaceButton = () => {
15
  const [isSignedIn, setIsSignedIn] = useState(false);
16
  const [isLoading, setIsLoading] = useState(false);
17
  const [name, setName] = useState<string | null>(null);
18
 
19
- useEffect(() => {
20
- const idToken = window.localStorage.getItem("huggingface_id_token");
21
- const accessToken = window.localStorage.getItem("huggingface_access_token");
22
 
23
- if (idToken && accessToken) {
24
- const idTokenObject = JSON.parse(idToken);
25
- if (idTokenObject.exp > Date.now() / 1000) {
26
- setIsSignedIn(true);
27
- setName(idTokenObject.name);
28
-
29
- return;
30
- }
31
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
 
 
 
 
 
 
 
 
 
33
 
 
 
 
 
34
  async function fetchToken() {
35
- const code = new URLSearchParams(window.location.search).get("code");
36
- if (code) {
37
- // remove the code from the url
38
- window.history.replaceState({}, "", window.location.pathname);
39
- setIsLoading(true);
40
- const response = await fetch(`${OAUTH_API_BASE}`, {
41
- method: "POST",
42
- headers: {
43
- "Content-Type": "application/x-www-form-urlencoded",
44
- Authorization: `Basic ${btoa(`${CLIENT_ID}:${CLIENT_SECRET}`)}`,
45
- },
46
- body: new URLSearchParams({
47
- client_id: CLIENT_ID,
48
- code,
49
- grant_type: "authorization_code",
50
- redirect_uri: REDIRECT_URI
51
- }).toString(),
52
- });
53
- const data = await response.json();
54
- window.localStorage.setItem("huggingface_access_token", data.access_token);
55
-
56
- // parse the id_token
57
- const idToken = jwtDecode(data.id_token);
58
- console.log(idToken);
59
- window.localStorage.setItem("huggingface_id_token", JSON.stringify(idToken));
60
- setName(idToken.name);
61
- setIsSignedIn(true);
62
- setIsLoading(false);
63
- }
64
  }
65
 
66
  fetchToken();
 
5
 
6
  const CLIENT_ID = "a67ef241-fb7e-4300-a6bd-8430a7565c9a";
7
  const REDIRECT_URI = "https://huggingfacetb-wikispeedia.hf.space";
8
+ // const REDIRECT_URI = "http://localhost:5173/auth/callback";
9
  const SCOPE = "openid%20profile%20email%20inference-api";
10
  const STATE = "1234567890";
11
  const SSO_URL = `https://huggingface.co/oauth/authorize?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=${SCOPE}&prompt=consent&state=${STATE}`;
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);
22
  const [isLoading, setIsLoading] = useState(false);
23
  const [name, setName] = useState<string | null>(null);
24
 
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) {
32
+ // setIsSignedIn(true);
33
+ // setName(idTokenObject.name);
34
+
35
+ // return;
36
+ // }
37
+ // }
38
+
39
+
40
+ // async function fetchToken() {
41
+ // const code = new URLSearchParams(window.location.search).get("code");
42
+ // if (code) {
43
+ // // remove the code from the url
44
+ // window.history.replaceState({}, "", window.location.pathname);
45
+ // setIsLoading(true);
46
+ // const response = await fetch(`${OAUTH_API_BASE}`, {
47
+ // method: "POST",
48
+ // headers: {
49
+ // "Content-Type": "application/x-www-form-urlencoded",
50
+ // Authorization: `Basic ${btoa(`${CLIENT_ID}:${CLIENT_SECRET}`)}`,
51
+ // },
52
+ // body: new URLSearchParams({
53
+ // client_id: CLIENT_ID,
54
+ // code,
55
+ // grant_type: "authorization_code",
56
+ // redirect_uri: REDIRECT_URI
57
+ // }).toString(),
58
+ // });
59
+ // const data = await response.json();
60
+ // window.localStorage.setItem("huggingface_access_token", data.access_token);
61
 
62
+ // // parse the id_token
63
+ // const idToken = jwtDecode(data.id_token);
64
+ // console.log(idToken);
65
+ // window.localStorage.setItem("huggingface_id_token", JSON.stringify(idToken));
66
+ // setName(idToken.name);
67
+ // setIsSignedIn(true);
68
+ // setIsLoading(false);
69
+ // }
70
+ // }
71
 
72
+ // fetchToken();
73
+ // }, []);
74
+
75
+ useEffect(() => {
76
  async function fetchToken() {
77
+ console.log("fetching token", window.location.href);
78
+ const oauthResult = await oauthHandleRedirectIfPresent();
79
+
80
+ if (!oauthResult) {
81
+ // If the user is not logged in, redirect to the login page
82
+ window.location.href = await oauthLoginUrl();
83
+ }
84
+
85
+ // You can use oauthResult.accessToken, oauthResult.accessTokenExpiresAt and oauthResult.userInfo
86
+ console.log(oauthResult);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  }
88
 
89
  fetchToken();