File size: 1,531 Bytes
5ecbfe5
 
2c217a5
 
 
 
 
 
 
 
 
 
 
bc6eb2f
5ecbfe5
bc6eb2f
5ecbfe5
2c217a5
 
 
 
 
 
 
5ecbfe5
 
bc6eb2f
5ecbfe5
bc6eb2f
2c217a5
 
bc6eb2f
5ecbfe5
7bb3a2b
 
2c217a5
 
 
 
 
 
 
 
 
 
 
 
 
 
bc6eb2f
 
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
import { oauthLoginUrl, oauthHandleRedirectIfPresent } from "@huggingface/hub";

// Get UI elements
const signinButton = document.getElementById("signin") as HTMLImageElement;
const signoutButton = document.getElementById("signout") as HTMLButtonElement;
const preElement = document.querySelector("pre") as HTMLPreElement;

// Show/hide UI elements based on auth state
function updateUI(isLoggedIn: boolean) {
  signinButton.style.display = isLoggedIn ? "none" : "block";
  signoutButton.style.display = isLoggedIn ? "block" : "none";
}

async function handleOAuth() {
  try {
    const oauthResult = await oauthHandleRedirectIfPresent();

    if (oauthResult) {
      // We have OAuth results, display them
      preElement.textContent = JSON.stringify(oauthResult, null, 2);
      updateUI(true);
    } else {
      // No OAuth results, show sign-in button
      updateUI(false);
    }

    return oauthResult;
  } catch (error) {
    console.error("OAuth error:", error);
    preElement.textContent = "Error: " + (error as Error).message;
    updateUI(false);
    throw error;
  }
}

// Handle sign-in click
signinButton.addEventListener("click", async () => {
  const loginUrl = await oauthLoginUrl();
  window.location.href = loginUrl;
});

// Handle sign-out click
signoutButton.addEventListener("click", () => {
  // Clear the display and reset UI
  preElement.textContent = "";
  updateUI(false);
  // You might want to add additional logout logic here
});

// Initialize OAuth handling
handleOAuth().catch(console.error);