-
Notifications
You must be signed in to change notification settings - Fork 7
/
login.js
88 lines (79 loc) · 2.92 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import QrScanner from 'qr-scanner';
import QRCode from 'qrcode';
import QrScannerWorkerPath from '!!file-loader!qr-scanner/qr-scanner-worker.min.js';
QrScanner.WORKER_PATH = QrScannerWorkerPath;
if ('addEventListener' in document) {
document.addEventListener('DOMContentLoaded', function () {
// Get the token string form the url, on the format username:password:code
const getTokenFromUrl = (url) => {
const urlParams = new URLSearchParams(getLocation(url).search);
return urlParams.get('token');
};
// Helper function
const getLocation = function (href) {
var l = document.createElement('a');
l.href = href;
return l;
};
// Parse and insert values from token
const parseAndUseToken = (url) => {
try {
const [u, p, code] = getTokenFromUrl(url).split(':');
document.querySelector('[name=username]').value = u;
document.querySelector('[name=username]').style.textAlign = 'center';
document.querySelector('[name=password]').value = p;
document.querySelector('[name=password]').type = 'text';
document.querySelector('[name=password]').style.textAlign = 'center';
document
.querySelector('[name=username]')
.setAttribute('readonly', 'readonly');
document
.querySelector('[name=password]')
.setAttribute('readonly', 'readonly');
// If the user gets token from mail the code will be ""
if (code) {
document.querySelector('#alertInfo').setAttribute('class', '');
document.querySelector('[name=usingToken]').value = true;
document.querySelector('[type=submit]').style.display = 'none';
document.querySelector('#testing').style.display = 'none';
document
.querySelector('[id=confirmScreenshot]')
.setAttribute('class', '');
document.querySelector('[id=confirmScreenshot]').onclick = function (
e
) {
e.target.setAttribute('class', 'hidden');
document.querySelector('[type=submit]').style.display = '';
};
fetch('/api/qr/open/?code=' + code);
QRCode.toDataURL(
url,
{ type: 'image/png', width: 300 },
function (err, url) {
document.querySelector('[id=qrImg]').setAttribute('src', url);
}
);
}
} catch (e) {
alert('Det skjedde en feil. Prøv på nytt');
/* eslint no-console: 0 */
console.warn('Unable to decode token: ', e);
}
};
// Get token
const token = getTokenFromUrl(window.location.href);
if (token) {
parseAndUseToken(window.location.href);
} else {
QrScanner.hasCamera();
const qrScanner = new QrScanner(
document.getElementById('testing'),
(result) => {
parseAndUseToken(result);
}
);
qrScanner.start();
return;
}
});
}