-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
43 lines (33 loc) · 1.13 KB
/
auth.js
File metadata and controls
43 lines (33 loc) · 1.13 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
function checkAuth() {
const sessionUser = sessionStorage.getItem('sessionUser');
return !!sessionUser;
}
function login(username, password) {
const users = JSON.parse(localStorage.getItem('users')) || {};
if (users[username] && users[username].password === password) {
sessionStorage.setItem('sessionUser', username);
return true;
}
return false;
}
function register(username, email, password) {
const users = JSON.parse(localStorage.getItem('users')) || {};
const existingUsers = Object.values(users);
const usernameExists = users[username] !== undefined;
const emailExists = Object.values(users).some(user => user.email === email);
if (usernameExists || emailExists) {
return false;
}
users[username] = { email: email, password: password };
localStorage.setItem('users', JSON.stringify(users));
return true;
}
function logout() {
sessionStorage.removeItem('sessionUser');
window.location.href = 'index.html';
}
if (window.location.pathname.endsWith('home.html')) {
if (!checkAuth()) {
window.location.href = 'index.html';
}
}