-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount.html
More file actions
221 lines (194 loc) · 7.14 KB
/
account.html
File metadata and controls
221 lines (194 loc) · 7.14 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>MyScratchBlocks Account | Login</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
body {
font-family: 'Inter', sans-serif;
background-color: #f8fafc;
color: #334155;
min-height: 100vh;
}
</style>
</head>
<body class="antialiased flex items-center justify-center">
<main class="w-full max-w-md p-8 bg-white rounded-lg shadow-lg">
<h2 class="text-3xl font-bold mb-8 text-center text-indigo-600">Login / Register</h2>
<div id="authBox">
<label for="username" class="block mb-1 text-sm font-medium text-gray-700">Username</label>
<input
type="text"
id="username"
placeholder="Enter your username"
autocomplete="username"
class="w-full mb-4 px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-indigo-500"
/>
<div id="emailGroup">
<label for="email" class="block mb-1 text-sm font-medium text-gray-700">Email</label>
<input
type="email"
id="email"
placeholder="Enter your email"
autocomplete="email"
class="w-full mb-4 px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-indigo-500"
/>
</div>
<label for="password" class="block mb-1 text-sm font-medium text-gray-700">Password</label>
<input
type="password"
id="password"
placeholder="Enter your password"
autocomplete="current-password"
class="w-full mb-4 px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-indigo-500"
/>
<p id="error" class="text-red-600 mb-4 hidden"></p>
<div class="flex justify-between space-x-4">
<button
onclick="register()"
class="flex-1 bg-indigo-600 hover:bg-indigo-700 text-white font-semibold py-2 rounded-md transition"
>
Register
</button>
<button
onclick="login()"
class="flex-1 bg-indigo-500 hover:bg-indigo-600 text-white font-semibold py-2 rounded-md transition"
>
Login
</button>
</div>
</div>
<div id="dashboard" class="hidden mt-8 p-6 bg-indigo-50 rounded-lg shadow-inner text-indigo-900">
<h3 class="text-2xl font-semibold mb-3">Welcome, <span id="userDisplay"></span>!</h3>
<p class="mb-6">Email: <span id="emailDisplay"></span></p>
<div class="flex justify-between space-x-4">
<button
onclick="home()"
class="flex-1 bg-indigo-600 hover:bg-indigo-700 text-white font-semibold py-2 rounded-md transition"
>
Home
</button>
<button
onclick="mystuff()"
class="flex-1 bg-indigo-600 hover:bg-indigo-700 text-white font-semibold py-2 rounded-md transition"
>
My Stuff
</button>
<button
onclick="logout()"
class="flex-1 bg-red-600 hover:bg-red-700 text-white font-semibold py-2 rounded-md transition"
>
Logout
</button>
</div>
</div>
</main>
<script>
const API_BASE = "/api";
// Hide/show email input on login/register
const emailGroup = document.getElementById("emailGroup");
function showError(msg) {
const err = document.getElementById("error");
err.textContent = msg;
err.classList.remove("hidden");
}
function hideError() {
const err = document.getElementById("error");
err.textContent = "";
err.classList.add("hidden");
}
async function register() {
hideError();
emailGroup.style.display = "block"; // show email for register
const username = document.getElementById("username").value.trim();
const email = document.getElementById("email").value.trim();
const password = document.getElementById("password").value;
if (!username || !email || !password) {
return showError("All fields are required.");
}
try {
const res = await fetch(`${API_BASE}/register`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, email, password }),
});
const data = await res.json();
if (!res.ok) {
showError(data.error || "Registration failed.");
} else {
localStorage.setItem("username", data.username || username);
localStorage.setItem("email", data.email || email);
showDashboard();
}
} catch {
showError("Network error. Please try again.");
}
}
async function login() {
hideError();
emailGroup.style.display = "none"; // hide email on login
const username = document.getElementById("username").value.trim();
const password = document.getElementById("password").value;
if (!username || !password) {
return showError("Username and password are required.");
}
try {
const res = await fetch(`${API_BASE}/login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }),
});
const data = await res.json();
if (!res.ok || !data.message) {
showError(data.error || "Login failed.");
} else {
localStorage.setItem("username", username);
// Email unknown on login, fallback to stored or empty
if (!localStorage.getItem("email")) localStorage.setItem("email", "");
showDashboard();
}
} catch {
showError("Network error. Please try again.");
}
}
function showDashboard() {
const username = localStorage.getItem("username");
const email = localStorage.getItem("email") || "No email available";
if (!username) {
logout();
return;
}
document.getElementById("userDisplay").textContent = username;
document.getElementById("emailDisplay").textContent = email;
document.getElementById("authBox").classList.add("hidden");
document.getElementById("dashboard").classList.remove("hidden");
}
function logout() {
localStorage.clear();
document.getElementById("dashboard").classList.add("hidden");
document.getElementById("authBox").classList.remove("hidden");
hideError();
// Clear inputs
document.getElementById("username").value = "";
document.getElementById("email").value = "";
document.getElementById("password").value = "";
emailGroup.style.display = "block"; // show email by default (for registration)
}
window.onload = () => {
emailGroup.style.display = "block"; // show email input by default for registration
if (localStorage.getItem("username")) {
showDashboard();
}
};
function home() {
window.location.href = "/";
}
function mystuff() {
window.location.href = "/mystuff";
}
</script>
</body>
</html>