change paths, add task to db
This commit is contained in:
parent
6a56b03bf6
commit
9e058e4f03
7 changed files with 87 additions and 70 deletions
|
@ -13,6 +13,10 @@ func migrateInitial(orm *gorm.DB) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(fmt.Errorf("failed to migrate User"))
|
fmt.Println(fmt.Errorf("failed to migrate User"))
|
||||||
}
|
}
|
||||||
|
err = orm.AutoMigrate(&Task{})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(fmt.Errorf("failed to migrate Task"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Database) Initialize() {
|
func (db *Database) Initialize() {
|
||||||
|
|
|
@ -14,3 +14,9 @@ type User struct {
|
||||||
Password string
|
Password string
|
||||||
LoggedIn bool
|
LoggedIn bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Task struct {
|
||||||
|
ID int `gorm:"primaryKey"`
|
||||||
|
Description string
|
||||||
|
Done bool
|
||||||
|
}
|
||||||
|
|
|
@ -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 id="button-tasks" class="btn btn-primary" href="/tasks">Tasks</a>
|
<a hidden id="button-tasks" class="btn btn-primary" href="/view/tasks">Tasks</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item me-2">
|
<li class="nav-item me-2">
|
||||||
<a id="button-login" class="btn btn-primary" href="/login">Login</a>
|
<a id="button-login" class="btn btn-primary" href="/view/login">Login</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<button id="button-logout" class="btn btn-danger" onclick="logout()">Logout</button>
|
<button hidden id="button-logout" class="btn btn-danger" onclick="logout()">Logout</button>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
@ -96,8 +96,13 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
async function userLoggedIn() {
|
async function userLoggedIn() {
|
||||||
const response = await fetch("/user", {method: 'GET', headers: myHeaders});
|
const response = await fetch("/auth/user", {method: 'GET', headers: myHeaders});
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
|
if (json.logged_in === true) {
|
||||||
|
document.getElementById("button-login").hidden = true;
|
||||||
|
button_logout.hidden = false;
|
||||||
|
document.getElementById("button-tasks").hidden = false;
|
||||||
|
}
|
||||||
return json.logged_in
|
return json.logged_in
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,7 +110,7 @@
|
||||||
button_logout.disabled = true;
|
button_logout.disabled = true;
|
||||||
let data = new FormData();
|
let data = new FormData();
|
||||||
data.append("username", username);
|
data.append("username", username);
|
||||||
const response = await fetch("/logout", {method: 'POST', body: data});
|
const response = await fetch("/auth/logout", {method: 'POST', body: data});
|
||||||
deleteCookie("username");
|
deleteCookie("username");
|
||||||
response.ok && redirect("/");
|
response.ok && redirect("/");
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,13 +21,13 @@
|
||||||
userLoggedIn().then((loggedIn) => loggedIn && redirect("/"));
|
userLoggedIn().then((loggedIn) => loggedIn && redirect("/"));
|
||||||
|
|
||||||
async function submitForm(formData) {
|
async function submitForm(formData) {
|
||||||
const response = await fetch("/login", {method: 'POST', body: formData, redirect: 'follow'});
|
const response = await fetch("/auth/login", {method: 'POST', body: formData, redirect: 'follow'});
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
setCookie("username", json.username, 1);
|
setCookie("username", json.username, 1);
|
||||||
redirect("/");
|
redirect("/");
|
||||||
} else {
|
} else {
|
||||||
redirect("/login");
|
redirect("/view/login");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -19,11 +19,11 @@
|
||||||
{{template "formScripts" .}}
|
{{template "formScripts" .}}
|
||||||
<script>
|
<script>
|
||||||
async function submitForm(formData) {
|
async function submitForm(formData) {
|
||||||
const response = await fetch("/register", {method: 'POST', body: formData, redirect: 'follow'});
|
const response = await fetch("/auth/register", {method: 'POST', body: formData, redirect: 'follow'});
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
redirect("/");
|
redirect("/");
|
||||||
} else {
|
} else {
|
||||||
redirect("/register");
|
redirect("/view/register");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
{{template "scripts" .}}
|
{{template "scripts" .}}
|
||||||
<script>
|
<script>
|
||||||
userLoggedIn().then((loggedIn) => !loggedIn && redirect("/login"));
|
userLoggedIn().then((loggedIn) => !loggedIn && redirect("/view/login"));
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -8,71 +8,73 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (wp *Webpage) redirectHome(c *gin.Context) {
|
func (wp *Webpage) redirectHome(c *gin.Context) {
|
||||||
c.Redirect(http.StatusTemporaryRedirect, "/tasks")
|
c.Redirect(http.StatusTemporaryRedirect, "/view/tasks")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wp *Webpage) defineRoutes() {
|
func (wp *Webpage) defineRoutes() {
|
||||||
|
|
||||||
wp.Router.GET("/tasks", func(c *gin.Context) {
|
view := wp.Router.Group("/view")
|
||||||
c.HTML(http.StatusOK, "tasks.tmpl", gin.H{"title": "Tasks"})
|
{
|
||||||
})
|
view.GET("/tasks", func(c *gin.Context) {
|
||||||
wp.Router.GET("/login", func(c *gin.Context) {
|
c.HTML(http.StatusOK, "tasks.tmpl", gin.H{"title": "Tasks"})
|
||||||
c.HTML(http.StatusOK, "login.tmpl", gin.H{"title": "Login"})
|
})
|
||||||
})
|
view.GET("/login", func(c *gin.Context) {
|
||||||
wp.Router.GET("/register", func(c *gin.Context) {
|
c.HTML(http.StatusOK, "login.tmpl", gin.H{"title": "Login"})
|
||||||
c.HTML(http.StatusOK, "register.tmpl", gin.H{"title": "Register"})
|
})
|
||||||
})
|
view.GET("/register", func(c *gin.Context) {
|
||||||
|
c.HTML(http.StatusOK, "register.tmpl", gin.H{"title": "Register"})
|
||||||
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
|
|
||||||
}
|
|
||||||
c.JSON(200, gin.H{"logged_in": false, "username": ""})
|
|
||||||
})
|
|
||||||
|
|
||||||
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) {
|
|
||||||
username, uExisting := c.GetPostForm("username")
|
|
||||||
password, pExisting := c.GetPostForm("password")
|
|
||||||
if uExisting == false || pExisting == false || username == "" || password == "" {
|
|
||||||
c.JSON(400, gin.H{"message": "bad post form"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
success := wp.Database.LoginUser(username, password)
|
|
||||||
if success == true {
|
|
||||||
c.JSON(200, gin.H{"logged_in": success, "username": username})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.JSON(401, gin.H{"message": "user or password not found"})
|
|
||||||
})
|
|
||||||
|
|
||||||
wp.Router.POST("/register", func(c *gin.Context) {
|
|
||||||
username, uExisting := c.GetPostForm("username")
|
|
||||||
password, pExisting := c.GetPostForm("password")
|
|
||||||
if uExisting == false || pExisting == false || username == "" || password == "" {
|
|
||||||
c.JSON(400, gin.H{"message": "bad post form"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err := wp.Database.CreateUser(username, password)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(500, gin.H{"message": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.JSON(200, gin.H{"message": "user registered"})
|
|
||||||
})
|
|
||||||
|
|
||||||
|
auth := wp.Router.Group("/auth")
|
||||||
|
{
|
||||||
|
auth.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
|
||||||
|
}
|
||||||
|
c.JSON(200, gin.H{"logged_in": false, "username": ""})
|
||||||
|
})
|
||||||
|
auth.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})
|
||||||
|
})
|
||||||
|
auth.POST("/login", func(c *gin.Context) {
|
||||||
|
username, uExisting := c.GetPostForm("username")
|
||||||
|
password, pExisting := c.GetPostForm("password")
|
||||||
|
if uExisting == false || pExisting == false || username == "" || password == "" {
|
||||||
|
c.JSON(400, gin.H{"message": "bad post form"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
success := wp.Database.LoginUser(username, password)
|
||||||
|
if success == true {
|
||||||
|
c.JSON(200, gin.H{"logged_in": success, "username": username})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(401, gin.H{"message": "user or password not found"})
|
||||||
|
})
|
||||||
|
auth.POST("/register", func(c *gin.Context) {
|
||||||
|
username, uExisting := c.GetPostForm("username")
|
||||||
|
password, pExisting := c.GetPostForm("password")
|
||||||
|
if uExisting == false || pExisting == false || username == "" || password == "" {
|
||||||
|
c.JSON(400, gin.H{"message": "bad post form"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err := wp.Database.CreateUser(username, password)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(500, gin.H{"message": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, gin.H{"message": "user registered"})
|
||||||
|
})
|
||||||
|
}
|
||||||
wp.Router.NoRoute(func(c *gin.Context) {
|
wp.Router.NoRoute(func(c *gin.Context) {
|
||||||
wp.redirectHome(c)
|
wp.redirectHome(c)
|
||||||
})
|
})
|
||||||
|
|
Reference in a new issue