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"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="ms-auto navbar-nav mb-2 mb-lg-0"> <ul class="ms-auto navbar-nav mb-2 mb-lg-0">
<li class="nav-item me-2"> <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>
<li class="nav-item me-2"> <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>
<li class="nav-item"> <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> </li>
</ul> </ul>
</div> </div>
@ -64,10 +64,10 @@
{{define "scripts"}} {{define "scripts"}}
<script> <script>
const button_login = document.getElementById("button-login");
const button_logout = document.getElementById("button-logout"); const button_logout = document.getElementById("button-logout");
const button_tasks = document.getElementById("button-tasks");
const username = getCookie("username"); const username = getCookie("username");
const myHeaders = new Headers();
myHeaders.append('username', username);
function setCookie(cookie_name, cookie_value, cookie_expiry) { function setCookie(cookie_name, cookie_value, cookie_expiry) {
const d = new Date(); const d = new Date();
@ -96,19 +96,8 @@
} }
async function userLoggedIn() { async function userLoggedIn() {
let data = new FormData(); const response = await fetch("/user", {method: 'GET', headers: myHeaders});
data.append("username", username);
const response = await fetch("/user", {method: 'POST', body: data});
const json = await response.json(); 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 return json.logged_in
} }

View file

@ -21,8 +21,6 @@
async function submitForm(formData) { async function submitForm(formData) {
const response = await fetch("/register", {method: 'POST', body: formData, redirect: 'follow'}); const response = await fetch("/register", {method: 'POST', body: formData, redirect: 'follow'});
if (response.ok) { if (response.ok) {
const json = await response.json();
console.log(json);
redirect("/"); redirect("/");
} else { } else {
redirect("/register"); redirect("/register");

View file

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