File size: 2,342 Bytes
a9b2457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Login with Hugging Face OAuth
import { oauthHandleRedirectIfPresent, oauthLoginUrl } from "@huggingface/hub";
import { InferenceClient } from "@huggingface/inference";
import { useEffect, useRef, useState } from "react";

export default function OAuthLogin({ onHFClientReady }) {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");
  const [oauthResult, setOAuthResult] = useState(null);
  const [userInfo, setUserInfo] = useState(null);
  const oauthHandled = useRef(false);
  const stableOnHFClientReady = useRef(onHFClientReady);

  useEffect(() => {
    // Prevent handling OAuth twice in Strict Mode
    if (oauthHandled.current) return;
    oauthHandled.current = true;

    (async () => {
      setLoading(true);
      try {
        const result = await oauthHandleRedirectIfPresent();
        if (!result) return;
        setOAuthResult(result);
        setError("");
      } catch (err) {
        setError(err.message);
      } finally {
        setLoading(false);
      }
    })();
  }, []);

  useEffect(() => {
    stableOnHFClientReady.current = onHFClientReady;
  }, [onHFClientReady]);

  useEffect(() => {
    if (oauthResult) {
      try {
        const client = new InferenceClient(oauthResult.accessToken);
        stableOnHFClientReady.current(client);
        setUserInfo(oauthResult.userInfo);
        setError("");
      } catch (err) {
        setError(err.message);
      }
    }
  }, [oauthResult]);

  const handleLogin = async () => {
    if (window.location.hostname.endsWith(".hf.space")) {
      window.location.href = await oauthLoginUrl();
    } else {
      window.location.href = await oauthLoginUrl({
        clientId: "d9a24fb2-1e7c-4a57-ace3-c75fa6a99305",
        redirectUrl: window.location.origin,
        scopes: ["inference-api"],
      });
    }
  };

  return (
    <div style={{ padding: "20px" }}>
      {userInfo ? (
        <div>
          <p>
            ✅ {userInfo.name || userInfo.email} has successfully logged in!
          </p>
        </div>
      ) : loading ? (
        <p>🔄 Authenticating with Hugging Face…</p>
      ) : (
        <button onClick={handleLogin} style={{ padding: "10px 20px" }}>
          Login with Hugging Face
        </button>
      )}
      {error && <p style={{ color: "red" }}>{error}</p>}
    </div>
  );
}