/
register.js
109 lines (83 loc) · 3.16 KB
/
register.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
const attachListeners = () => {
console.log('Attaching event listeners...')
document.getElementById('register-form').addEventListener('submit', submitRegisterForm)
document.getElementById('two-factor-method-form').addEventListener('submit', submit2FAMethodForm)
document.getElementById('qr-code-form').addEventListener('submit', submitQRCodeForm)
document.getElementById('sms-form').addEventListener('submit', submitSMSForm)
document.getElementById('sms').addEventListener('click', showPhoneNumberField)
document.getElementById('totp').addEventListener('click', hidePhoneNumberField)
}
const showPhoneNumberField = () => {
document.getElementById('phone-number-control').style.display = 'block'
}
const hidePhoneNumberField = () => {
document.getElementById('phone-number-control').style.display = 'none'
}
const submitRegisterForm = (e) => {
e.preventDefault()
const username = input('username')
const password = input('password')
axios.post(url('register'), { username, password })
.then(proceedToTwoFactorMethodForm)
.catch(() => alert('nope'))
}
const proceedToTwoFactorMethodStep = (method) =>
method === 'totp'
? showQRCodeForm
: showSMSForm
const proceedToTwoFactorMethodForm = (res) => {
localStorage.setItem('userId', res.data.userId)
document.getElementById('register-form').style.display = 'none'
document.getElementById('two-factor-method-form').style.display = 'block'
}
const showQRCodeForm = (res) => {
const qrCodeForm = document.getElementById('qr-code-form')
document.getElementById('two-factor-method-form').style.display = 'none'
qrCode = document.createElement('img')
qrCode.src = res.data.qr
qrCodeForm.querySelector('.qr-image').appendChild(qrCode)
qrCodeForm.style.display = 'block'
}
const showSMSForm = (res) => {
document.getElementById('phone-number-show').innerHTML = res.data.phoneNumber
document.getElementById('sms-form').style.display = 'block'
document.getElementById('two-factor-method-form').style.display = 'none'
}
const submit2FAMethodForm = (e) => {
e.preventDefault()
const method = document.querySelector('input[name="two-factor-method"]:checked').value
const phoneNumber = input('phone_number')
const userId = localStorage.getItem('userId')
axios
.post(url('auth/setup'), { method, phoneNumber, userId })
.then(proceedToTwoFactorMethodStep(method))
.catch(() => alert('oops'))
}
const showQRCodeError = () => {
document.getElementById('qr-code-error').style.display = 'block'
}
const showSMSCodeError = () => {
document.getElementById('sms-code-error').style.display = 'block'
}
const submitQRCodeForm = (e) => {
e.preventDefault()
const token = input('token')
const userId = localStorage.getItem('userId')
axios
.post(url('auth/validate'), { userId, token })
.then(login)
.catch(showQRCodeError)
}
const submitSMSForm = (e) => {
e.preventDefault()
const token = input('sms_token')
const userId = localStorage.getItem('userId')
axios
.post(url('auth/validate'), { method: 'sms', userId, token })
.then(login)
.catch(showSMSCodeError)
}
const setup = () => {
attachListeners()
}
document.addEventListener('DOMContentLoaded', setup)