/
login.js
70 lines (52 loc) · 1.81 KB
/
login.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
const attachListeners = () => {
console.log('Attaching event listeners...')
if (localStorage.getItem('logged_in')) {
document.getElementById('login-form').style.display = 'none'
document.getElementById('logged-in').style.display = 'block'
return
}
document.getElementById('login-form').addEventListener('submit', submitLoginForm)
document.getElementById('two-factor-form').addEventListener('submit', submit2FAForm)
}
const proceedToTwoFactorForm = (res) => {
localStorage.setItem('userId', res.data.userId)
document.getElementById('login-form').style.display = 'none'
document.getElementById('two-factor-form').style.display = 'block'
}
const showLoginError = () => {
document.getElementById('login-error').style.display = 'block'
}
const hideLoginError = () => {
document.getElementById('login-error').style.display = 'block'
}
const submitLoginForm = (e) => {
e.preventDefault()
hideLoginError()
const username = input('username')
const password = input('password')
axios
.post(url('login'), { username, password })
.then(proceedToTwoFactorForm)
.catch(showLoginError)
}
const showLoggedInPage = () => {
document.getElementById('logged-in-page').style.display = 'block'
document.getElementById('two-factor-form').style.display = 'none'
}
const showOneTimePasswordError = () => {
document.getElementById('one-time-password-error').style.display = 'block'
}
const submit2FAForm = (e) => {
e.preventDefault()
document.getElementById('one-time-password-error').style.display = 'none'
const token = input('token')
const userId = localStorage.getItem('userId')
axios
.post(url('auth/token'), { token, userId })
.then(login)
.catch(showOneTimePasswordError)
}
const setup = () => {
attachListeners()
}
document.addEventListener('DOMContentLoaded', setup)