really basic login

This commit is contained in:
Florian Hoss 2022-04-04 10:16:29 +02:00
parent 177a26a2e9
commit 539a36dffe
6 changed files with 116 additions and 50 deletions

View file

@ -9,10 +9,13 @@
{{template "navbar" .}}
<div class="position-absolute top-50 start-50 translate-middle text-center">
<div>Welcome to the {{ .title }}.</div>
<div>
<a class="link-secondary" href="/login">Login</a> to continue.
</div>
{{if .loggedIn}}
<div>Welcome to the {{ .title }}.</div>
{{else}}
<div>
<a class="link-secondary" href="/login">Login</a> to continue.
</div>
{{end}}
</div>
{{template "scripts" .}}
@ -42,9 +45,18 @@
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="ms-auto navbar-nav mb-2 mb-lg-0">
<li class="nav-item">
<a class="btn btn-primary" href="/login">Login</a>
</li>
{{if .loggedIn}}
<li class="nav-item me-2">
<a class="btn btn-primary" href="/tasks">Tasks</a>
</li>
<li class="nav-item">
<a class="btn btn-danger" href="/logout">Logout</a>
</li>
{{else}}
<li class="nav-item">
<a class="btn btn-primary" href="/login">Login</a>
</li>
{{end}}
</ul>
</div>
</div>
@ -101,5 +113,9 @@
form.classList.add('was-validated');
}, false);
}
function redirect(location) {
window.location.href = location;
}
</script>
{{end}}

View file

@ -17,13 +17,15 @@
{{template "scripts" .}}
<script>
function submitForm(formData) {
fetch("/login", {method: 'POST', body: formData, redirect: 'follow'})
.then(response => response.json())
.then((json) => {
console.log(json)
}
).catch(error => console.log(error));
async function submitForm(formData) {
const response = await fetch("/login", {method: 'POST', body: formData, redirect: 'follow'});
if (response.ok) {
const json = await response.json();
console.log("JSON:", json.message);
redirect("/");
} else {
redirect("/login");
}
}
</script>
</body>

View file

@ -18,13 +18,15 @@
{{template "scripts" .}}
<script>
function submitForm(formData) {
fetch("/register", {method: 'POST', body: formData, redirect: 'follow'})
.then(response => response.json())
.then(() => {
window.location.href = "/";
}
).catch(error => console.log(error));
async function submitForm(formData) {
const response = await fetch("/register", {method: 'POST', body: formData, redirect: 'follow'});
if (response.ok) {
const json = await response.json();
console.log("JSON:", json.message);
redirect("/");
} else {
redirect("/register");
}
}
</script>
</body>