-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.js
58 lines (48 loc) · 1.66 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
//signing up
function handleFormSubmit(event) {
event.preventDefault();
const username = document.getElementById('names').value;
const email = document.getElementById('email').value;
const password = document.getElementById('password1').value;
signUp(username, email, password);
}
function signUp(username, email, password) {
const formData = new FormData();
formData.append('username', username);
formData.append('email', email);
formData.append('password', password);
fetch('https://personal-web-backend-318j.onrender.com/api/signup', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.log('Sign Up successful:', data.message);
if (data.message === 'User created successfully') {
localStorage.setItem('isLoggedIn', 'true');
const token = data.token;
localStorage.setItem('Token', token);
showConfirmationMessage();
setTimeout(() => {
window.location.href = 'index.html';
}, 3000)
} else {
const confMessage = document.getElementById('confirmationMessage');
const innerMessage = data.message;
confMessage.innerText = innerMessage;
showConfirmationMessage();
return
}
})
.catch(error => {
console.error('Error signing up:', error);
});
}
function showConfirmationMessage() {
const confirmationMessage = document.getElementById('confirmationMessage');
confirmationMessage.style.display = 'flex';
setTimeout(() => {
confirmationMessage.style.display = 'none';
}, 4000);
}
document.getElementById('signupForm').addEventListener('submit', handleFormSubmit);