proxyauth: Use ajax instead of form submit

This commit is contained in:
Girish Ramakrishnan
2020-11-10 23:56:07 -08:00
parent 4cebc58576
commit 13ab5c1345

View File

@@ -59,23 +59,47 @@
<div class="container">
<h1 class="center">Cloudron Login</h1>
<form action="/login" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" autofocus=""/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td></td>
<td class="right"><input type="submit" value="Login" class="button"/></td>
</tr>
</table>
</form>
<table>
<tr>
<td>Username:</td>
<td><input type="text" id="username" autofocus=""/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" id="password"/></td>
</tr>
<tr>
<td></td>
<td class="right"><input type="submit" value="Login" id="login" class="button"/></td>
</tr>
</table>
<p id="message"></p>
</div>
<script>
function login() {
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
fetch('/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: username, password: password })
}).then(function (response) {
if (response.status === 401 || response.status === 403) {
document.getElementById('message').innerText = 'Invalid username or password';
return;
}
var search = decodeURIComponent(window.location.search).slice(1).split('&').map(function (item) { return item.split('='); }).reduce(function (o, k) { o[k[0]] = k[1]; return o; }, {});
window.location.href = search.redirect || '/';
});
}
document.getElementById('username').addEventListener('keyup', function (e) { if (e.key === 'Enter') login(); });
document.getElementById('password').addEventListener('keyup', function (e) { if (e.key === 'Enter') login(); });
document.getElementById('login').addEventListener('click', function (e) { login(); });
</script>
</body>
</html>