-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregistration.js
45 lines (39 loc) · 1.26 KB
/
registration.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
;(async function () {
let token
async function registration(data = {}) {
const response = await fetch('http://localhost:3030/users/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
token = response.headers.get('x-access-token')
return response.json()
}
const form = document.querySelector('form')
form.addEventListener('submit', async (event) => {
event.preventDefault()
const firstName = event.currentTarget.querySelector('#firstName').value
const lastName = event.currentTarget.querySelector('#lastName').value
const email = event.currentTarget.querySelector('#email').value
const password = event.currentTarget.querySelector('#password').value
const passwordConfirmation = event.currentTarget.querySelector(
'#passwordConfirmation'
).value
const data = {
firstName,
lastName,
email,
password,
passwordConfirmation,
}
const userData = await registration(data)
localStorage.setItem('x-access-token', token)
localStorage.setItem('user-data', JSON.stringify(userData))
window.location = window.location.pathname.replace(
'registration.html',
'index.html'
)
})
})()