-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.js
More file actions
114 lines (97 loc) · 3.71 KB
/
auth.js
File metadata and controls
114 lines (97 loc) · 3.71 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
// 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! Welcome back! 🎉');
setTimeout(() => {
window.location.href = '/';
}, 1500);
} else {
showAlert('Login failed: ' + data.message, 'error');
}
} catch (error) {
console.error('Login error:', error);
showAlert('An error occurred during login', '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();
const displayName = document.getElementById('display_name').value.trim();
const mobileNumber = document.getElementById('mobile_number').value.trim();
const location = document.getElementById('location').value.trim();
const bio = document.getElementById('bio').value.trim();
if (!username || !email || !password || !confirmPassword || !displayName || !mobileNumber || !location) {
showAlert('Please fill in all required 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;
}
if (!validateEmail(email)) {
showAlert('Please enter a valid email address', '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,
display_name: displayName,
mobile_number: mobileNumber,
location,
bio
})
});
const data = await response.json();
console.log("Registration response:", data);
if (data.success) {
showAlert('Registration successful! Welcome to our community! 🎉');
setTimeout(() => {
window.location.href = '/';
}, 2000);
} else {
showAlert('Registration failed: ' + data.message, 'error');
}
} catch (error) {
console.error('Registration error:', error);
showAlert('An error occurred during registration', 'error');
}
}
// Make functions globally available
window.login = login;
window.register = register;