Zain437 commited on
Commit
6417b9c
Β·
verified Β·
1 Parent(s): 7840acc

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +51 -103
index.html CHANGED
@@ -1,112 +1,60 @@
1
  <!DOCTYPE html>
2
  <html lang="en">
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width, initial-scale=1" />
6
- <title>Puter AI Chat</title>
7
- <link rel="stylesheet" href="style.css" />
8
- <script src="https://js.puter.com/v2/"></script>
9
- </head>
10
-
11
- <body>
12
- <div id="app">
13
- <header>
14
- <h1>πŸ’¬ Puter AI Chat</h1>
15
- <button id="sign-in">πŸ” Sign in</button>
16
- <span id="status-text">Not signed in</span>
17
- </header>
18
-
19
- <main id="chat-container">
20
- <div id="messages"></div>
21
- <div id="chat-input">
22
- <input
23
- type="text"
24
- id="input-message"
25
- placeholder="Type a message..."
26
- />
27
- <button id="send-btn">Send</button>
28
- </div>
29
- </main>
30
-
31
- <footer>
32
- <p>
33
- Built with ❀️ using <a href="https://puter.com" target="_blank">Puter.js</a> |
34
- <a href="https://huggingface.co/spaces/Zain437/puter" target="_blank">View Space</a>
35
- </p>
36
- </footer>
37
- </div>
38
-
39
- <script>
40
- const messages = [];
41
- const statusEl = document.getElementById("status-text");
42
-
43
- // 🌐 Check sign-in status on load
44
- async function checkSignInStatus() {
45
- const signedIn = await puter.auth.isSignedIn();
46
- if (signedIn) {
47
- const user = await puter.auth.getUser();
48
- statusEl.textContent = `Signed in as ${user.username}`;
49
  } else {
50
- statusEl.textContent = "Not signed in";
51
  }
52
  }
 
53
 
54
- checkSignInStatus();
55
-
56
- // πŸ” Sign in button
57
- document.getElementById("sign-in").addEventListener("click", async () => {
58
- try {
59
- statusEl.textContent = "Signing in...";
60
- const user = await puter.auth.signIn();
61
- statusEl.textContent = `βœ… Signed in as ${user.username}`;
62
- console.log("User:", user);
63
- } catch (error) {
64
- statusEl.textContent = "❌ Sign-in failed";
65
- console.error("Sign-in error:", error);
66
- }
67
- });
68
 
69
- // πŸ’¬ Chat system
70
- function addMessage(msg, isUser = false) {
71
- const messagesDiv = document.getElementById("messages");
72
- const messageDiv = document.createElement("div");
73
- messageDiv.classList.add("message");
74
- if (isUser) messageDiv.classList.add("user-message");
75
- messageDiv.textContent = msg;
76
- messagesDiv.appendChild(messageDiv);
77
- messagesDiv.scrollTop = messagesDiv.scrollHeight;
78
  }
79
-
80
- document.getElementById("send-btn").addEventListener("click", sendMessage);
81
- document.getElementById("input-message").addEventListener("keypress", (e) => {
82
- if (e.key === "Enter") sendMessage();
83
- });
84
-
85
- async function sendMessage() {
86
- const input = document.getElementById("input-message");
87
- const message = input.value.trim();
88
- if (!message) return;
89
-
90
- addMessage(message, true);
91
- input.value = "";
92
-
93
- messages.push({ role: "user", content: message });
94
-
95
- try {
96
- const response = await puter.ai.chat(messages);
97
- const reply = response.message?.content || JSON.stringify(response);
98
- addMessage(reply);
99
- messages.push({ role: "assistant", content: reply });
100
- } catch (err) {
101
- addMessage("⚠️ Error connecting to AI. Check console.");
102
- console.error("AI chat error:", err);
103
- }
104
- }
105
-
106
- // πŸ“Š Optional: Show usage in console
107
- puter.auth.getMonthlyUsage().then((usage) => {
108
- console.log("Usage:", usage);
109
- });
110
- </script>
111
- </body>
112
  </html>
 
1
  <!DOCTYPE html>
2
  <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Puter Auth Demo</title>
7
+ <link rel="stylesheet" href="style.css">
8
+ <script src="https://js.puter.com/v2/"></script>
9
+ </head>
10
+ <body>
11
+ <div class="container">
12
+ <h1>Puter Authentication</h1>
13
+ <p class="desc">Sign in to your Puter account to continue</p>
14
+
15
+ <button id="sign-in">Sign In</button>
16
+ <pre id="output">Waiting for user action...</pre>
17
+ </div>
18
+
19
+ <script>
20
+ const output = document.getElementById('output');
21
+ const signInBtn = document.getElementById('sign-in');
22
+
23
+ function print(msg) {
24
+ output.innerText = msg;
25
+ console.log(msg);
26
+ }
27
+
28
+ async function signInWithRedirectFallback() {
29
+ try {
30
+ print("Attempting popup sign-in...");
31
+ const user = await puter.auth.signIn();
32
+ print("βœ… Signed in:\n" + JSON.stringify(user, null, 2));
33
+ return user;
34
+ } catch (err) {
35
+ if (err.message?.includes("popup")) {
36
+ print("⚠️ Popup blocked or unavailable. Trying redirect login...");
37
+ try {
38
+ await puter.auth.signIn({ mode: 'redirect' });
39
+ } catch (redirectErr) {
40
+ print("❌ Redirect sign-in failed:\n" + JSON.stringify(redirectErr));
41
+ }
 
 
 
 
 
 
 
42
  } else {
43
+ print("❌ Sign-in error:\n" + JSON.stringify(err));
44
  }
45
  }
46
+ }
47
 
48
+ signInBtn.addEventListener('click', () => {
49
+ signInWithRedirectFallback();
50
+ });
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ (async () => {
53
+ const user = await puter.auth.getUser();
54
+ if (user) {
55
+ print("βœ… Already signed in:\n" + JSON.stringify(user, null, 2));
 
 
 
 
 
56
  }
57
+ })();
58
+ </script>
59
+ </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  </html>