-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (69 loc) · 1.95 KB
/
app.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
if (!navigator.credentials) {
alert('Credential Management API not supported')
}
const url = new URL(location.href)
if (url.pathname === '/' || url.pathname === '/index.html') {
credentialsFetch()
.then(credentials => loginCheck(credentials.id, credentials.password))
.catch(() => {
location.href = '/login.html'
})
} else if (url.pathname === '/login.html') {
document.getElementById('form-login').addEventListener('submit', loginSubmit)
}
/**
* @returns {Promise}
*/
function credentialsFetch() {
console.debug('CALL credentialsFetch')
if (!navigator.credentials) {
return Promise.reject(501)
}
return navigator.credentials
.get({password: true})
.then(credentials => credentials ? Promise.resolve(credentials) : Promise.reject(404))
}
/**
* @param {String} id
* @param {String} password
* @returns {Promise}
*/
function credentialsStore(id, password) {
console.debug('CALL credentialsStore')
if (!navigator.credentials) {
return Promise.reject(501)
}
const credentials = new PasswordCredential({id, password})
return navigator.credentials.store(credentials)
}
/**
* Fake authentication
* @param {String} username
* @param {String} password
* @returns {Promise}
*/
function loginCheck(username, password) {
if (username === 'myuser' && password === 'supersecret') {
return Promise.resolve(200)
}
return Promise.reject(401)
}
/**
* @param {Event} event
*/
function loginSubmit(event) {
console.debug('CALL loginSubmit')
event.preventDefault()
const username = document.getElementById('username').value
const password = document.getElementById('password').value
loginCheck(username, password)
.then(() => {
credentialsStore(username, password)
})
.then(() => {
location.href = 'index.html'
})
.catch(() => {
alert('Bad Credentials')
})
}