simpleauth/static/login.html

71 lines
2.0 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {
font-family: sans-serif;
color: white;
background: seagreen linear-gradient(315deg, rgba(255,255,255,0.2), transparent);
height: 100%;
}
div {
margin: 1em;
}
#error {
color: red;
}
</style>
<script>
function error(msg) {
document.querySelector("#error").textContent = msg
}
async function login(evt) {
evt.preventDefault()
let data = new FormData(evt.target)
let username = data.get("username")
let password = data.get("password")
url = new URL(evt.target.action)
url.username = ""
url.password = ""
let headers = new Headers({
"Authorization": "Basic " + btoa(username + ":" + password),
"X-Simpleauth-Login": "true",
})
let req = await fetch(url, {
method: "GET",
headers: headers,
credentials: "same-origin",
})
let token = req.headers.get("X-Simpleauth-Token")
if (token) {
// Set a cookie, just in case
document.cookie = `simpleauth-token=${token}; path=/; Secure; SameSite=Strict`
location.reload(true)
} else {
error(req.statusText || "Authentication failed")
}
}
async function init() {
document.querySelector("form").addEventListener("submit", login)
}
window.addEventListener("load", init)
</script>
</head>
<body>
<h1>Login</h1>
<form>
<div>Username: <input type="text" autocomplete="username" name="username"></div>
<div>Password: <input type="password" autocomplete="current-password" name="password"></div>
<div><input type="submit" value="Log In"></div>
</form>
<div id="error"></div>
</body>
</html>