Reaperxxxx commited on
Commit
b601cfa
·
verified ·
1 Parent(s): 2a3ed54

Update public/index.html

Browse files
Files changed (1) hide show
  1. public/index.html +65 -50
public/index.html CHANGED
@@ -65,68 +65,83 @@
65
 
66
  <script>
67
  function showSignUp() {
68
- document.getElementById("loginSection").style.display = "none";
69
- document.getElementById("signupSection").style.display = "block";
70
- }
71
-
72
- function showLogin() {
73
- document.getElementById("signupSection").style.display = "none";
74
- document.getElementById("loginSection").style.display = "block";
75
- }
76
 
77
- // Login handler
78
- document.getElementById("loginForm").addEventListener("submit", async (event) => {
79
- event.preventDefault();
 
 
80
 
81
- const username = document.getElementById("loginUsername").value.trim();
82
- const password = document.getElementById("loginPassword").value.trim();
 
83
 
84
- if (!username || !password) {
85
- alert("Both fields are required!");
86
- return;
87
- }
88
 
89
- const response = await fetch("/login", {
90
- method: "POST",
91
- headers: { "Content-Type": "application/json" },
92
- body: JSON.stringify({ username, password }),
93
- });
94
 
95
- if (response.ok) {
96
- alert("Login successful!");
97
- window.location.href = "/terminal.html"; // Redirect to terminal page
98
- } else {
99
- const message = await response.text();
100
- alert(`Login failed: ${message}`);
101
- }
102
  });
103
 
104
- // Sign-up handler
105
- document.getElementById("signupForm").addEventListener("submit", async (event) => {
106
- event.preventDefault();
 
 
 
 
 
 
 
 
 
 
 
107
 
108
- const username = document.getElementById("signupUsername").value.trim();
109
- const password = document.getElementById("signupPassword").value.trim();
 
110
 
111
- if (!username || !password) {
112
- alert("Both fields are required!");
113
- return;
114
- }
115
 
116
- const response = await fetch("/signup", {
117
- method: "POST",
118
- headers: { "Content-Type": "application/json" },
119
- body: JSON.stringify({ username, password }),
120
- });
121
 
122
- if (response.ok) {
123
- alert("Sign-up successful! Redirecting to terminal...");
124
- window.location.href = "/terminal.html"; // Redirect to terminal page
125
- } else {
126
- const message = await response.text();
127
- alert(`Sign-up failed: ${message}`);
128
- }
129
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  </script>
131
  </body>
132
  </html>
 
65
 
66
  <script>
67
  function showSignUp() {
68
+ document.getElementById("loginSection").style.display = "none";
69
+ document.getElementById("signupSection").style.display = "block";
70
+ }
 
 
 
 
 
71
 
72
+ // Show login form
73
+ function showLogin() {
74
+ document.getElementById("signupSection").style.display = "none";
75
+ document.getElementById("loginSection").style.display = "block";
76
+ }
77
 
78
+ // Login handler
79
+ document.getElementById("loginForm").addEventListener("submit", async (event) => {
80
+ event.preventDefault();
81
 
82
+ const username = document.getElementById("loginUsername").value.trim();
83
+ const password = document.getElementById("loginPassword").value.trim();
 
 
84
 
85
+ if (!username || !password) {
86
+ alert("Both fields are required!");
87
+ return;
88
+ }
 
89
 
90
+ try {
91
+ const response = await fetch("/login", {
92
+ method: "POST",
93
+ headers: { "Content-Type": "application/json" },
94
+ body: JSON.stringify({ username, password }),
 
 
95
  });
96
 
97
+ if (response.ok) {
98
+ const data = await response.json(); // Expecting { message, username }
99
+ localStorage.setItem("username", data.username); // Save username to localStorage
100
+ alert("Login successful!");
101
+ window.location.href = "/terminal.html"; // Redirect to terminal page
102
+ } else {
103
+ const message = await response.text();
104
+ alert(`Login failed: ${message}`);
105
+ }
106
+ } catch (error) {
107
+ console.error("Login error:", error);
108
+ alert("An unexpected error occurred. Please try again.");
109
+ }
110
+ });
111
 
112
+ // Sign-up handler
113
+ document.getElementById("signupForm").addEventListener("submit", async (event) => {
114
+ event.preventDefault();
115
 
116
+ const username = document.getElementById("signupUsername").value.trim();
117
+ const password = document.getElementById("signupPassword").value.trim();
 
 
118
 
119
+ if (!username || !password) {
120
+ alert("Both fields are required!");
121
+ return;
122
+ }
 
123
 
124
+ try {
125
+ const response = await fetch("/signup", {
126
+ method: "POST",
127
+ headers: { "Content-Type": "application/json" },
128
+ body: JSON.stringify({ username, password }),
 
 
129
  });
130
+
131
+ if (response.ok) {
132
+ const data = await response.json(); // Expecting a success message
133
+ localStorage.setItem("username", username); // Save username to localStorage
134
+ alert("Sign-up successful! Redirecting to terminal...");
135
+ window.location.href = "/terminal.html"; // Redirect to terminal page
136
+ } else {
137
+ const message = await response.text();
138
+ alert(`Sign-up failed: ${message}`);
139
+ }
140
+ } catch (error) {
141
+ console.error("Sign-up error:", error);
142
+ alert("An unexpected error occurred. Please try again.");
143
+ }
144
+ });
145
  </script>
146
  </body>
147
  </html>