summary refs log tree commit diff stats
path: root/main.go
diff options
context:
space:
mode:
authorckrinitsin <101062646+ckrinitsin@users.noreply.github.com>2025-04-24 23:54:17 +0200
committerGitHub <noreply@github.com>2025-04-24 23:54:17 +0200
commitb75c7c6a9a040af03ef8b498c88c267bfa00aaf4 (patch)
tree8b30f8b1269fe985a1fe0d5cca8b771a6e7484ff /main.go
parent1e974f70a6c262d0b5db8b177ebb02b46446bfb0 (diff)
downloadshopping-list-b75c7c6a9a040af03ef8b498c88c267bfa00aaf4.tar.gz
shopping-list-b75c7c6a9a040af03ef8b498c88c267bfa00aaf4.zip
Authentication (#1)
* add login and register templates

* add jwt and gin-contrib dependencies

* add List database table

* add authentication

* add logout
Diffstat (limited to 'main.go')
-rw-r--r--main.go21
1 files changed, 17 insertions, 4 deletions
diff --git a/main.go b/main.go
index 0553af0..ef20704 100644
--- a/main.go
+++ b/main.go
@@ -4,9 +4,13 @@ import (
 	"embed"
 	"html/template"
 	"net/http"
+	"os"
 
+	"github.com/ckrinitsin/shopping-list/authenticate"
 	"github.com/ckrinitsin/shopping-list/handlers"
 	"github.com/ckrinitsin/shopping-list/models"
+	"github.com/gin-contrib/sessions"
+	"github.com/gin-contrib/sessions/cookie"
 	"github.com/gin-gonic/gin"
 )
 
@@ -21,16 +25,25 @@ func main() {
 	tmpl := template.Must(template.ParseFS(templatesFS, "templates/*"))
 	r.SetHTMLTemplate(tmpl)
 
+	store := cookie.NewStore([]byte(os.Getenv("SECRET")))
+	r.Use(sessions.Sessions("session", store))
+
 	r.GET("/health", func(c *gin.Context) {
 		c.JSON(http.StatusOK, gin.H{
 			"health-check": "passed",
 		})
 	})
 
-	r.GET("/", shopping_list.LoadElements)
-	r.POST("/create", shopping_list.CreateEntry)
-	r.POST("/delete", shopping_list.DeleteEntries)
-	r.POST("/toggle", shopping_list.ToggleEntry)
+	r.POST("/login", authenticate.LoginPOST)
+	r.GET("/login", authenticate.LoginGET)
+	r.POST("/register", authenticate.RegisterPOST)
+	r.GET("/register", authenticate.RegisterGET)
+	r.POST("/logout", authenticate.Logout)
+
+	r.GET("/", authenticate.CheckAuth, shopping_list.LoadElements)
+	r.POST("/create", authenticate.CheckAuth, shopping_list.CreateEntry)
+	r.POST("/delete", authenticate.CheckAuth, shopping_list.DeleteEntries)
+	r.POST("/toggle", authenticate.CheckAuth, shopping_list.ToggleEntry)
 
 	r.Run()
 }