-
Notifications
You must be signed in to change notification settings - Fork 0
/
signUp.js
156 lines (135 loc) · 5.39 KB
/
signUp.js
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
document.addEventListener('DOMContentLoaded', function () {
// Select the sign-up form and add a submit event listener
const signUpForm = document.querySelector('form[action="/signUp"]');
signUpForm.addEventListener('submit', function (event) {
event.preventDefault();
// Call the validateForm function when the form is submitted
validateForm();
});
// Validate form input values
async function validateForm() {
// Get form input values
const fullName = document.getElementById('fullName').value;
const password = document.getElementById('password').value;
const passwordConfirm = document.getElementById('passwordConfirm').value;
// Reset any previous error messages
resetErrors();
// Check if the username already exists
const isUsernameExists = await checkUsernameExists(fullName);
if (isUsernameExists) {
displayError('Username already exists. Please choose a different one.');
return;
}
// Check if the full name is valid
if (!isValidFullName(fullName)) {
displayError('Invalid UserName');
}
// Check if passwords match
if (password !== passwordConfirm) {
displayError("Passwords don't Match");
}
// Check if the password meets length requirements
if (!isValidPasswordLength(password)) {
displayError('Make Password Longer');
}
// Check if the password contains at least one lowercase character
if (!isValidPasswordLowerCase(password)) {
displayError('Password Must Contain at Least One Lowercase');
}
// Call makeSignUp function if the form is valid
if (isFormValid(fullName, password, passwordConfirm)) {
const signUpData = {
username: fullName,
password: password,
accType: "Patron",
followers: [],
following: [],
reviews: []
};
// Make sign-up request
makeSignUp('/signUp', signUpData)
.then(responseData => {
// Handle successful response, if needed
window.location.href = responseData.redirectTo;
})
.catch(error => {
// Handle error
alert(error.message);
});
}
}
// Check if the form is valid
function isFormValid(fullName, password, passwordConfirm) {
return (
isValidFullName(fullName) &&
password === passwordConfirm &&
isValidPasswordLength(password) &&
isValidPasswordLowerCase(password)
);
}
// Validate full name
function isValidFullName(fullName) {
// Implement your validation logic for full name
// Example: Check if there are one or more letters followed by a space, followed by at least one more letter
const regex = /^[a-zA-Z0-9]+$/;
return regex.test(fullName);
}
// Validate password length
function isValidPasswordLength(password) {
// Implement your validation logic for password length
// Example: Check if the password has 10 to 20 characters
return password.length >= 10 && password.length <= 20;
}
// Validate at least one lowercase character in the password
function isValidPasswordLowerCase(password) {
// Implement your validation logic for at least one lowercase character
// Example: Use a regular expression
const regex = /[a-z]/;
return regex.test(password);
}
// Reset error messages
function resetErrors() {
const errorContainer = document.getElementById('errorContainer');
errorContainer.textContent = '';
}
// Display error message
function displayError(errorMessage) {
const errorContainer = document.getElementById('errorContainer');
const errorItem = document.createElement('p');
errorItem.textContent = errorMessage;
errorContainer.appendChild(errorItem);
}
// Make sign-up request
async function makeSignUp(url, data) {
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// Parse the JSON response
const responseData = await response.json();
return responseData;
} catch (error) {
throw new Error(`Failed to make sign-up request: ${error.message}`);
}
}
// Check if username exists
async function checkUsernameExists(username) {
try {
const response = await fetch(`/checkUsernameExists?username=${username}`);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data.exists; // Assumes the server responds with a JSON object containing a boolean property 'exists'
} catch (error) {
throw new Error(`Failed to check if the username exists: ${error.message}`);
}
}
});