DSDUDEd commited on
Commit
52a726a
Β·
verified Β·
1 Parent(s): 97fbbbc

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +62 -3
script.js CHANGED
@@ -22,6 +22,31 @@ document.addEventListener("DOMContentLoaded", () => {
22
  chatContainer.scrollTop = chatContainer.scrollHeight;
23
  }
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  // ------------------
26
  // Login Handler
27
  // ------------------
@@ -35,12 +60,16 @@ document.addEventListener("DOMContentLoaded", () => {
35
  method: "POST",
36
  headers: {"Content-Type": "application/json"},
37
  body: JSON.stringify({ email, password }),
38
- credentials: "include" // <-- ensures session cookie is stored
39
  });
40
  const data = await res.json();
41
 
42
  if (data.error) {
43
- alert("❌ " + data.error);
 
 
 
 
44
  } else {
45
  alert("βœ… " + data.message);
46
  loginForm.style.display = "none";
@@ -52,6 +81,36 @@ document.addEventListener("DOMContentLoaded", () => {
52
  }
53
  };
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  // ------------------
56
  // Chat Handler
57
  // ------------------
@@ -72,7 +131,7 @@ document.addEventListener("DOMContentLoaded", () => {
72
  method: "POST",
73
  headers: {"Content-Type": "application/json"},
74
  body: JSON.stringify({ message, model: modelChoice, max_tokens: maxTokens }),
75
- credentials: "include" // <-- important for login session
76
  });
77
  const data = await res.json();
78
 
 
22
  chatContainer.scrollTop = chatContainer.scrollHeight;
23
  }
24
 
25
+ // ------------------
26
+ // Show phone verification prompt
27
+ // ------------------
28
+ async function handlePhoneVerification(email) {
29
+ const code = prompt("Enter the verification code sent to your phone:");
30
+ if (!code) return alert("Verification code required.");
31
+
32
+ try {
33
+ const res = await fetch("/api/verify_phone", {
34
+ method: "POST",
35
+ headers: {"Content-Type": "application/json"},
36
+ body: JSON.stringify({ email, code })
37
+ });
38
+ const data = await res.json();
39
+ if (data.error) {
40
+ alert("❌ " + data.error);
41
+ } else {
42
+ alert("βœ… " + data.message);
43
+ }
44
+ } catch (err) {
45
+ alert("❌ Could not verify phone.");
46
+ console.error(err);
47
+ }
48
+ }
49
+
50
  // ------------------
51
  // Login Handler
52
  // ------------------
 
60
  method: "POST",
61
  headers: {"Content-Type": "application/json"},
62
  body: JSON.stringify({ email, password }),
63
+ credentials: "include"
64
  });
65
  const data = await res.json();
66
 
67
  if (data.error) {
68
+ if (data.error === "Phone not verified") {
69
+ await handlePhoneVerification(email);
70
+ } else {
71
+ alert("❌ " + data.error);
72
+ }
73
  } else {
74
  alert("βœ… " + data.message);
75
  loginForm.style.display = "none";
 
81
  }
82
  };
83
 
84
+ // ------------------
85
+ // Signup Handler
86
+ // ------------------
87
+ document.getElementById("signup-btn")?.addEventListener("click", async () => {
88
+ const email = document.getElementById("signup-email").value.trim();
89
+ const username = document.getElementById("signup-username").value.trim();
90
+ const password = document.getElementById("signup-password").value.trim();
91
+ const phone = document.getElementById("signup-phone").value.trim();
92
+ if (!email || !username || !password || !phone) return alert("All fields are required.");
93
+
94
+ try {
95
+ const res = await fetch("/api/signup", {
96
+ method: "POST",
97
+ headers: {"Content-Type": "application/json"},
98
+ body: JSON.stringify({ email, username, password, phone }),
99
+ credentials: "include"
100
+ });
101
+ const data = await res.json();
102
+ if (data.error) {
103
+ alert("❌ " + data.error);
104
+ } else {
105
+ alert("βœ… " + data.message);
106
+ await handlePhoneVerification(email);
107
+ }
108
+ } catch (err) {
109
+ alert("❌ Signup failed: Could not reach server");
110
+ console.error(err);
111
+ }
112
+ });
113
+
114
  // ------------------
115
  // Chat Handler
116
  // ------------------
 
131
  method: "POST",
132
  headers: {"Content-Type": "application/json"},
133
  body: JSON.stringify({ message, model: modelChoice, max_tokens: maxTokens }),
134
+ credentials: "include"
135
  });
136
  const data = await res.json();
137