hanyuu/assets/oauth2.js/main.js

38 lines
1.2 KiB
JavaScript
Raw Normal View History

#include verify.js
(() => {
const authoriseButtons = document.querySelectorAll('.js-authorise-action');
for(const button of authoriseButtons) {
button.disabled = false;
button.onclick = () => {
for(const other of authoriseButtons)
other.disabled = true;
const body = [];
for(const name in button.dataset)
body.push(encodeURIComponent(name) + '=' + encodeURIComponent(button.dataset[name]));
const xhr = new XMLHttpRequest;
xhr.responseType = 'json';
xhr.open('POST', '/oauth2/authorise');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = () => {
if(xhr.response.redirect)
location.assign(xhr.response.redirect);
else
location.assign('/oauth2/error?error=invalid_request');
};
xhr.onerror = () => {
for(const other of authoriseButtons)
other.disabled = false;
};
xhr.send(body.join('&'));
};
}
if(location.pathname === '/oauth2/verify')
HanyuuOAuth2Verify();
})();