change post to get request with header

This commit is contained in:
Florian Hoss 2022-04-04 11:53:05 +02:00
parent 2d98665f92
commit 6a56b03bf6
3 changed files with 12 additions and 25 deletions

View file

@ -22,13 +22,13 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="ms-auto navbar-nav mb-2 mb-lg-0">
<li class="nav-item me-2">
<a hidden id="button-tasks" class="btn btn-primary" href="/tasks">Tasks</a>
<a id="button-tasks" class="btn btn-primary" href="/tasks">Tasks</a>
</li>
<li class="nav-item me-2">
<a hidden id="button-login" class="btn btn-primary" href="/login">Login</a>
<a id="button-login" class="btn btn-primary" href="/login">Login</a>
</li>
<li class="nav-item">
<button hidden id="button-logout" class="btn btn-danger" onclick="logout()">Logout</button>
<button id="button-logout" class="btn btn-danger" onclick="logout()">Logout</button>
</li>
</ul>
</div>
@ -64,10 +64,10 @@
{{define "scripts"}}
<script>
const button_login = document.getElementById("button-login");
const button_logout = document.getElementById("button-logout");
const button_tasks = document.getElementById("button-tasks");
const username = getCookie("username");
const myHeaders = new Headers();
myHeaders.append('username', username);
function setCookie(cookie_name, cookie_value, cookie_expiry) {
const d = new Date();
@ -96,19 +96,8 @@
}
async function userLoggedIn() {
let data = new FormData();
data.append("username", username);
const response = await fetch("/user", {method: 'POST', body: data});
const response = await fetch("/user", {method: 'GET', headers: myHeaders});
const json = await response.json();
if (json.logged_in === true) {
button_login.hidden = true;
button_logout.hidden = false;
button_tasks.hidden = false;
} else {
button_login.hidden = false;
button_logout.hidden = true;
button_tasks.hidden = true;
}
return json.logged_in
}

View file

@ -21,8 +21,6 @@
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);
redirect("/");
} else {
redirect("/register");

View file

@ -23,14 +23,14 @@ func (wp *Webpage) defineRoutes() {
c.HTML(http.StatusOK, "register.tmpl", gin.H{"title": "Register"})
})
wp.Router.POST("/user", func(c *gin.Context) {
username, uExisting := c.GetPostForm("username")
if uExisting == false || username == "" {
c.JSON(400, gin.H{"message": "bad post form"})
wp.Router.GET("/user", func(c *gin.Context) {
username := c.Request.Header.Get("username")
if username != "" {
success := wp.Database.UserIsLoggedIn(username)
c.JSON(200, gin.H{"logged_in": success, "username": username})
return
}
success := wp.Database.UserIsLoggedIn(username)
c.JSON(200, gin.H{"logged_in": success, "username": username})
c.JSON(200, gin.H{"logged_in": false, "username": ""})
})
wp.Router.POST("/logout", func(c *gin.Context) {