Repository navigation
Expand file tree
/
Copy pathauth.html
More file actions
180 lines (166 loc) · 4.95 KB
/
Copy pathauth.html
File metadata and controls
180 lines (166 loc) · 4.95 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Auth System</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f3f4f6;
position: relative;
}
.top-right {
position: absolute;
top: 10px;
right: 10px;
}
.container {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 300px;
}
h2 {
text-align: center;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
}
input {
width: 100%;
padding: 8px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
width: 100%;
padding: 10px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.error {
color: red;
font-size: 0.9em;
}
</style>
</head>
<body>
<div class="top-right">
<a href="index.html">Back to Home</a>
</div>
<div class="container" id="auth-container">
<h2>Login</h2>
<div id="form-container">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" placeholder="Enter your password">
</div>
<button onclick="handleLogin()">Login</button>
<p class="error" id="error-msg"></p>
<p>Don't have an account? <a href="#" onclick="showRegisterForm()">Register here</a></p>
</div>
</div>
<script>
const USERS_KEY = 'users';
function getUsers() {
return JSON.parse(localStorage.getItem(USERS_KEY)) || [];
}
function saveUsers(users) {
localStorage.setItem(USERS_KEY, JSON.stringify(users));
}
function handleLogin() {
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
const errorMsg = document.getElementById('error-msg');
errorMsg.textContent = '';
if (!email || !password) {
errorMsg.textContent = 'Please fill in all fields.';
return;
}
const users = getUsers();
const user = users.find(u => u.email === email && u.password === password);
if (user) {
alert('Login successful!');
} else {
errorMsg.textContent = 'Invalid email or password.';
}
}
function handleRegister() {
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
const errorMsg = document.getElementById('error-msg');
errorMsg.textContent = '';
if (!email || !password) {
errorMsg.textContent = 'Please fill in all fields.';
return;
}
const users = getUsers();
if (users.some(u => u.email === email)) {
errorMsg.textContent = 'User already exists.';
return;
}
users.push({ email, password });
saveUsers(users);
alert('Registration successful! You can now log in.');
showLoginForm();
}
function showRegisterForm() {
document.getElementById('auth-container').innerHTML = `
<h2>Register</h2>
<div id="form-container">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" placeholder="Enter your password">
</div>
<button onclick="handleRegister()">Register</button>
<p class="error" id="error-msg"></p>
<p>Already have an account? <a href="#" onclick="showLoginForm()">Login here</a></p>
</div>
`;
}
function showLoginForm() {
document.getElementById('auth-container').innerHTML = `
<h2>Login</h2>
<div id="form-container">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" placeholder="Enter your password">
</div>
<button onclick="handleLogin()">Login</button>
<p class="error" id="error-msg"></p>
<p>Don't have an account? <a href="#" onclick="showRegisterForm()">Register here</a></p>
</div>
`;
}
</script>
</body>
</html>