-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.js
73 lines (59 loc) · 1.99 KB
/
user.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
const displayErrorMessage = (message) => {
if (document.getElementById('error-message')) {
const m = document.getElementById('error-message')
m.parentNode.removeChild(m)
}
const msg = document.createElement('p')
msg.id = "error-message"
msg.textContent = message
msg.classList.add('shake')
const node = document.getElementById('btn')
const parent = node.parentNode
parent.insertBefore(msg, node)
}
const sendFormData = () => {
return new Promise( function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://192.168.71.203:5000/employee_details');
// This fires up when the connection is successful
xhr.onload = function(event){
const res = JSON.parse(xhr.response)
console.log(xhr.response)
var statusMessage = res.status ? res.status : res.msg
if (xhr.status == 200) {
resolve(xhr.status)
} else {
reject(statusMessage)
}
};
var token = localStorage.getItem('token')
token = `Bearer ${token}`
const form = document.getElementById("signup-form")
const formData = new FormData(form);
xhr.setRequestHeader('Authorization', token)
xhr.send(formData);
})
}
const checkIfLoggedIn = () => {
const token = localStorage.getItem('token')
if (!token) {
window.location.href = './login.html'
}
}
checkIfLoggedIn()
document.getElementById('submit').addEventListener('click', async () => {
try {
var status = await sendFormData()
} catch (error) {
alert(error)
if (error.slice(-7,) == "expired") {
localStorage.clear()
window.location.href = './login.html'
}
} finally {
if (status == 200) {
alert("User successfully created")
window.location.href = "./index.html"
}
}
})