-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathverify auth.js
More file actions
97 lines (81 loc) · 3.03 KB
/
verify auth.js
File metadata and controls
97 lines (81 loc) · 3.03 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
// static/js/auth.js
console.log("auth.js loaded successfully!");
async function login() {
console.log("Login function called");
const username = document.getElementById('username').value.trim();
const password = document.getElementById('password').value.trim();
console.log("Username:", username);
console.log("Password:", password);
if (!username || !password) {
showAlert('Please fill in all fields', 'error');
return;
}
try {
console.log("Sending login request...");
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username, password })
});
const data = await response.json();
console.log("Login response:", data);
if (data.success) {
showAlert('Login successful!');
setTimeout(() => {
window.location.href = '/';
}, 1000);
} else {
showAlert('Login failed: ' + data.message, 'error');
}
} catch (error) {
console.error('Login error:', error);
showAlert('An error occurred during login: ' + error.message, 'error');
}
}
async function register() {
console.log("Register function called");
const username = document.getElementById('username').value.trim();
const email = document.getElementById('email').value.trim();
const password = document.getElementById('password').value.trim();
const confirmPassword = document.getElementById('confirm-password').value.trim();
if (!username || !email || !password || !confirmPassword) {
showAlert('Please fill in all fields', 'error');
return;
}
if (password !== confirmPassword) {
showAlert('Passwords do not match', 'error');
return;
}
if (password.length < 6) {
showAlert('Password must be at least 6 characters long', 'error');
return;
}
try {
console.log("Sending registration request...");
const response = await fetch('/api/auth/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username, email, password })
});
const data = await response.json();
console.log("Registration response:", data);
if (data.success) {
showAlert('Registration successful!');
setTimeout(() => {
window.location.href = '/';
}, 1000);
} else {
showAlert('Registration failed: ' + data.message, 'error');
}
} catch (error) {
console.error('Registration error:', error);
showAlert('An error occurred during registration: ' + error.message, 'error');
}
}
// Make functions globally available
window.login = login;
window.register = register;