save username in cookie

This commit is contained in:
Florian Hoss 2022-04-04 11:40:34 +02:00
parent 539a36dffe
commit 2d98665f92
7 changed files with 149 additions and 95 deletions

View file

@ -30,14 +30,31 @@ func (db *Database) CreateUser(username string, password string) error {
return result.Error
}
func (db *Database) LoginUser(username string, password string) (bool, error) {
func (db *Database) LoginUser(username string, password string) bool {
user := User{Username: username, Password: password}
result := db.ORM.Where("username = ? AND password = ?", username, password).Find(&user)
if result.Error != nil {
return false, result.Error
}
if result.RowsAffected == 1 {
return true, nil
db.ORM.Model(&user).Update("logged_in", true)
return true
}
return false, nil
return false
}
func (db *Database) LogoutUser(username string) bool {
user := User{Username: username}
result := db.ORM.Where("username = ? and logged_in = ?", username, true).Find(&user)
if result.RowsAffected == 1 {
db.ORM.Model(&user).Update("logged_in", false)
return true
}
return false
}
func (db *Database) UserIsLoggedIn(username string) bool {
user := User{Username: username}
result := db.ORM.Where("username = ? and logged_in = ?", username, true).Find(&user)
if result.RowsAffected == 1 {
return true
}
return false
}

View file

@ -12,5 +12,5 @@ type Database struct {
type User struct {
Username string `gorm:"primaryKey"`
Password string
CreatedAt int64 `gorm:"autoCreateTime"`
LoggedIn bool
}

View file

@ -1,27 +1,3 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ .title }}</title>
{{template "head" .}}
</head>
<body>
{{template "navbar" .}}
<div class="position-absolute top-50 start-50 translate-middle text-center">
{{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" .}}
</body>
</html>
{{define "head"}}
<link rel="apple-touch-icon" sizes="180x180" href="../static/icons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="../static/icons/favicon-32x32.png">
@ -37,7 +13,7 @@
{{define "navbar"}}
<nav class="navbar navbar-expand-md navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="/">SuperSave</a>
<a class="navbar-brand" href="/">SuperSafe</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
@ -45,18 +21,15 @@
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="ms-auto navbar-nav mb-2 mb-lg-0">
{{if .loggedIn}}
<li class="nav-item me-2">
<a class="btn btn-primary" href="/tasks">Tasks</a>
<a hidden 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>
</li>
<li class="nav-item">
<a class="btn btn-danger" href="/logout">Logout</a>
<button hidden id="button-logout" class="btn btn-danger" onclick="logout()">Logout</button>
</li>
{{else}}
<li class="nav-item">
<a class="btn btn-primary" href="/login">Login</a>
</li>
{{end}}
</ul>
</div>
</div>
@ -90,9 +63,67 @@
{{end}}
{{define "scripts"}}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>
<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");
function setCookie(cookie_name, cookie_value, cookie_expiry) {
const d = new Date();
d.setTime(d.getTime() + (cookie_expiry * 24 * 60 * 60 * 1000));
let expires = "expires=" + d.toUTCString();
document.cookie = cookie_name + "=" + cookie_value + ";" + expires + ";path=/;SameSite=Lax"
}
function deleteCookie(cookie_name) {
document.cookie = cookie_name + "=" + ";expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/;SameSite=Lax"
}
function getCookie(cookie_name) {
let name = cookie_name + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let cookieArray = decodedCookie.split('; ');
let result;
cookieArray.forEach(val => {
if (val.indexOf(name) === 0) result = val.substring(name.length);
})
return result;
}
function redirect(location) {
window.location.href = location;
}
async function userLoggedIn() {
let data = new FormData();
data.append("username", username);
const response = await fetch("/user", {method: 'POST', body: data});
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
}
async function logout() {
button_logout.disabled = true;
let data = new FormData();
data.append("username", username);
const response = await fetch("/logout", {method: 'POST', body: data});
deleteCookie("username");
response.ok && redirect("/");
}
</script>
{{end}}
{{define "formScripts"}}
<script>
let form = document.querySelector('.needs-validation')
if (form) {
@ -113,9 +144,5 @@
form.classList.add('was-validated');
}, false);
}
function redirect(location) {
window.location.href = location;
}
</script>
{{end}}

View file

@ -16,12 +16,15 @@
</div>
{{template "scripts" .}}
{{template "formScripts" .}}
<script>
userLoggedIn().then((loggedIn) => loggedIn && redirect("/"));
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);
setCookie("username", json.username, 1);
redirect("/");
} else {
redirect("/login");

View file

@ -15,14 +15,14 @@
<a class="link-secondary" href="/login">Login instead</a>
</div>
{{template "scripts" .}}
{{template "formScripts" .}}
<script>
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);
console.log(json);
redirect("/");
} else {
redirect("/register");

View file

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ .title }}</title>
{{template "head" .}}
</head>
<body>
{{template "navbar" .}}
<div class="position-absolute top-50 start-50 translate-middle">
<div class="fs-3">{{ .title }}</div>
<hr>
</div>
{{template "scripts" .}}
<script>
userLoggedIn().then((loggedIn) => !loggedIn && redirect("/login"));
</script>
</body>
</html>

View file

@ -8,47 +8,39 @@ import (
)
func (wp *Webpage) redirectHome(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, "/")
}
func (wp *Webpage) userIsLoggedIn(c *gin.Context) bool {
if wp.isLoggedIn {
wp.redirectHome(c)
return true
}
return false
c.Redirect(http.StatusTemporaryRedirect, "/tasks")
}
func (wp *Webpage) defineRoutes() {
wp.Router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "SuperSafe",
"loggedIn": wp.isLoggedIn,
wp.Router.GET("/tasks", func(c *gin.Context) {
c.HTML(http.StatusOK, "tasks.tmpl", gin.H{"title": "Tasks"})
})
wp.Router.GET("/login", func(c *gin.Context) {
c.HTML(http.StatusOK, "login.tmpl", gin.H{"title": "Login"})
})
wp.Router.GET("/register", func(c *gin.Context) {
c.HTML(http.StatusOK, "register.tmpl", gin.H{"title": "Register"})
})
wp.Router.GET("/login", func(c *gin.Context) {
if wp.userIsLoggedIn(c) {
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"})
return
}
c.HTML(http.StatusOK, "login.tmpl", gin.H{
"title": "Login",
"loggedIn": wp.isLoggedIn,
})
success := wp.Database.UserIsLoggedIn(username)
c.JSON(200, gin.H{"logged_in": success, "username": username})
})
wp.Router.GET("/logout", func(c *gin.Context) {
wp.isLoggedIn = false
wp.redirectHome(c)
})
wp.Router.GET("/register", func(c *gin.Context) {
wp.userIsLoggedIn(c)
c.HTML(http.StatusOK, "register.tmpl", gin.H{
"title": "Register",
"loggedIn": wp.isLoggedIn,
})
wp.Router.POST("/logout", func(c *gin.Context) {
username, uExisting := c.GetPostForm("username")
if uExisting == false || username == "" {
c.JSON(400, gin.H{"message": "bad post form"})
return
}
success := wp.Database.LogoutUser(username)
c.JSON(200, gin.H{"logged_out": success, "username": username})
})
wp.Router.POST("/login", func(c *gin.Context) {
@ -58,17 +50,11 @@ func (wp *Webpage) defineRoutes() {
c.JSON(400, gin.H{"message": "bad post form"})
return
}
success, err := wp.Database.LoginUser(username, password)
if err != nil {
c.JSON(500, gin.H{"message": err.Error()})
return
}
success := wp.Database.LoginUser(username, password)
if success == true {
wp.isLoggedIn = true
c.JSON(200, gin.H{"message": "login successfull"})
c.JSON(200, gin.H{"logged_in": success, "username": username})
return
}
wp.isLoggedIn = false
c.JSON(401, gin.H{"message": "user or password not found"})
})