From 36bccce021f2d5ccc353017fa6790c325669a555 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 2 Dec 2021 20:04:18 +0000 Subject: [PATCH] Remove token from data and add to gin context --- http/accounts.go | 18 +++++++++--------- http/budget.go | 9 --------- http/dashboard.go | 10 +--------- http/http.go | 5 +++-- 4 files changed, 13 insertions(+), 29 deletions(-) diff --git a/http/accounts.go b/http/accounts.go index fa1b497..99251c9 100644 --- a/http/accounts.go +++ b/http/accounts.go @@ -3,24 +3,16 @@ package http import ( "net/http" - "git.javil.eu/jacob1123/budgeteer" "git.javil.eu/jacob1123/budgeteer/postgres" "github.com/gin-gonic/gin" "github.com/google/uuid" ) type AccountData struct { - Token budgeteer.Token Accounts []postgres.Account } func (h *Handler) accounts(c *gin.Context) { - token, err := h.verifyLogin(c) - if err != nil { - c.Redirect(http.StatusTemporaryRedirect, "/login") - return - } - budgetID := c.Param("budgetid") budgetUUID, err := uuid.Parse(budgetID) if err != nil { @@ -35,9 +27,17 @@ func (h *Handler) accounts(c *gin.Context) { } d := AccountData{ - Token: token, Accounts: accounts, } c.HTML(http.StatusOK, "accounts.html", d) } + +type AdminData struct { +} + +func (h *Handler) admin(c *gin.Context) { + d := AdminData{} + + c.HTML(http.StatusOK, "accounts.html", d) +} diff --git a/http/budget.go b/http/budget.go index 1958478..5116e7d 100644 --- a/http/budget.go +++ b/http/budget.go @@ -4,25 +4,17 @@ import ( "context" "net/http" - "git.javil.eu/jacob1123/budgeteer" "git.javil.eu/jacob1123/budgeteer/postgres" "github.com/gin-gonic/gin" "github.com/google/uuid" ) type BudgetData struct { - Token budgeteer.Token Budget *postgres.Budget Transactions []postgres.Transaction } func (h *Handler) budget(c *gin.Context) { - token, err := h.verifyLogin(c) - if err != nil { - c.Redirect(http.StatusTemporaryRedirect, "/login") - return - } - budgetID := c.Param("budgetid") budgetUUID, err := uuid.Parse(budgetID) if err != nil { @@ -43,7 +35,6 @@ func (h *Handler) budget(c *gin.Context) { } d := BudgetData{ - Token: token, Budget: &budget, Transactions: transactions, } diff --git a/http/dashboard.go b/http/dashboard.go index 52aea3d..81af500 100644 --- a/http/dashboard.go +++ b/http/dashboard.go @@ -9,26 +9,18 @@ import ( ) func (h *Handler) dashboard(c *gin.Context) { - token, err := h.verifyLogin(c) - if err != nil { - c.Redirect(http.StatusTemporaryRedirect, "/login") - return - } - - userID := token.GetID() + userID := c.MustGet("token").(budgeteer.Token).GetID() budgets, err := h.Service.BudgetsForUser(userID) if err != nil { return } d := DashboardData{ - Token: token, Budgets: budgets, } c.HTML(http.StatusOK, "dashboard.html", d) } type DashboardData struct { - Token budgeteer.Token Budgets []postgres.Budget } diff --git a/http/http.go b/http/http.go index 22177fa..7369bf2 100644 --- a/http/http.go +++ b/http/http.go @@ -54,6 +54,7 @@ func (h *Handler) Serve() { authenticatedFrontend.GET("/dashboard", h.dashboard) authenticatedFrontend.GET("/budget/:budgetid", h.budget) authenticatedFrontend.GET("/budget/:budgetid/accounts", h.accounts) + authenticatedFrontend.GET("/admin", h.admin) } api := router.Group("/api/v1") { @@ -126,17 +127,17 @@ func (h *Handler) importYNAB(c *gin.Context) { } func (h *Handler) verifyLoginWithRedirect(c *gin.Context) { - _, err := h.verifyLogin(c) + token, err := h.verifyLogin(c) if err != nil { c.Redirect(http.StatusTemporaryRedirect, "/login") return } + c.Set("token", token) c.Next() } func (h *Handler) newTransaction(c *gin.Context) { - transactionMemo, succ := c.GetPostForm("memo") if !succ { c.AbortWithStatus(http.StatusNotAcceptable)