-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
79 lines (66 loc) · 2.38 KB
/
auth.js
File metadata and controls
79 lines (66 loc) · 2.38 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
const API_URL = 'http://localhost:5000'; // Update this to your backend URL
// Mock Database (You can replace this with real backend integration later)
const users = [];
// Handle Sign-Up
async function handleSignUp(event) {
event.preventDefault();
const username = document.getElementById("username").value;
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
// Prepare the data to be sent
const userData = {
username: username,
email: email,
password: password
};
try {
const response = await fetch(`${API_URL}/api/users/register`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(userData)
});
const result = await response.json();
if (response.ok) {
alert(result.message); // Show success message
window.location.href = "login.html"; // Redirect to login page
} else {
alert(result.error); // Show error message
console.error('Error during registration:', result.error);
}
} catch (error) {
console.error('Error during registration:', error);
alert('An error occurred during registration. Please try again.');
}
}
// Handle Login
async function handleLogin(event) {
event.preventDefault();
const email = document.getElementById("loginUsername").value;
const password = document.getElementById("loginPassword").value;
const loginData = {
emailOrUsername: email,
password: password
};
try {
const response = await fetch(`${API_URL}/api/users/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(loginData)
});
const result = await response.json();
if (response.ok) {
alert(result.message); // Show success message
window.location.href = "index.html"; // Redirect to homepage
} else {
alert(result.error); // Show error message
console.error('Error during login:', result.error);
}
} catch (error) {
console.error('Error during login:', error);
alert('An error occurred during login. Please try again.');
}
}