Remove token from data and add to gin context

This commit is contained in:
Jan Bader 2021-12-02 20:04:18 +00:00
parent e7c9a7f52f
commit 36bccce021
4 changed files with 13 additions and 29 deletions

View File

@ -3,24 +3,16 @@ package http
import ( import (
"net/http" "net/http"
"git.javil.eu/jacob1123/budgeteer"
"git.javil.eu/jacob1123/budgeteer/postgres" "git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid" "github.com/google/uuid"
) )
type AccountData struct { type AccountData struct {
Token budgeteer.Token
Accounts []postgres.Account Accounts []postgres.Account
} }
func (h *Handler) accounts(c *gin.Context) { 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") budgetID := c.Param("budgetid")
budgetUUID, err := uuid.Parse(budgetID) budgetUUID, err := uuid.Parse(budgetID)
if err != nil { if err != nil {
@ -35,9 +27,17 @@ func (h *Handler) accounts(c *gin.Context) {
} }
d := AccountData{ d := AccountData{
Token: token,
Accounts: accounts, Accounts: accounts,
} }
c.HTML(http.StatusOK, "accounts.html", d) 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)
}

View File

@ -4,25 +4,17 @@ import (
"context" "context"
"net/http" "net/http"
"git.javil.eu/jacob1123/budgeteer"
"git.javil.eu/jacob1123/budgeteer/postgres" "git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid" "github.com/google/uuid"
) )
type BudgetData struct { type BudgetData struct {
Token budgeteer.Token
Budget *postgres.Budget Budget *postgres.Budget
Transactions []postgres.Transaction Transactions []postgres.Transaction
} }
func (h *Handler) budget(c *gin.Context) { 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") budgetID := c.Param("budgetid")
budgetUUID, err := uuid.Parse(budgetID) budgetUUID, err := uuid.Parse(budgetID)
if err != nil { if err != nil {
@ -43,7 +35,6 @@ func (h *Handler) budget(c *gin.Context) {
} }
d := BudgetData{ d := BudgetData{
Token: token,
Budget: &budget, Budget: &budget,
Transactions: transactions, Transactions: transactions,
} }

View File

@ -9,26 +9,18 @@ import (
) )
func (h *Handler) dashboard(c *gin.Context) { func (h *Handler) dashboard(c *gin.Context) {
token, err := h.verifyLogin(c) userID := c.MustGet("token").(budgeteer.Token).GetID()
if err != nil {
c.Redirect(http.StatusTemporaryRedirect, "/login")
return
}
userID := token.GetID()
budgets, err := h.Service.BudgetsForUser(userID) budgets, err := h.Service.BudgetsForUser(userID)
if err != nil { if err != nil {
return return
} }
d := DashboardData{ d := DashboardData{
Token: token,
Budgets: budgets, Budgets: budgets,
} }
c.HTML(http.StatusOK, "dashboard.html", d) c.HTML(http.StatusOK, "dashboard.html", d)
} }
type DashboardData struct { type DashboardData struct {
Token budgeteer.Token
Budgets []postgres.Budget Budgets []postgres.Budget
} }

View File

@ -54,6 +54,7 @@ func (h *Handler) Serve() {
authenticatedFrontend.GET("/dashboard", h.dashboard) authenticatedFrontend.GET("/dashboard", h.dashboard)
authenticatedFrontend.GET("/budget/:budgetid", h.budget) authenticatedFrontend.GET("/budget/:budgetid", h.budget)
authenticatedFrontend.GET("/budget/:budgetid/accounts", h.accounts) authenticatedFrontend.GET("/budget/:budgetid/accounts", h.accounts)
authenticatedFrontend.GET("/admin", h.admin)
} }
api := router.Group("/api/v1") api := router.Group("/api/v1")
{ {
@ -126,17 +127,17 @@ func (h *Handler) importYNAB(c *gin.Context) {
} }
func (h *Handler) verifyLoginWithRedirect(c *gin.Context) { func (h *Handler) verifyLoginWithRedirect(c *gin.Context) {
_, err := h.verifyLogin(c) token, err := h.verifyLogin(c)
if err != nil { if err != nil {
c.Redirect(http.StatusTemporaryRedirect, "/login") c.Redirect(http.StatusTemporaryRedirect, "/login")
return return
} }
c.Set("token", token)
c.Next() c.Next()
} }
func (h *Handler) newTransaction(c *gin.Context) { func (h *Handler) newTransaction(c *gin.Context) {
transactionMemo, succ := c.GetPostForm("memo") transactionMemo, succ := c.GetPostForm("memo")
if !succ { if !succ {
c.AbortWithStatus(http.StatusNotAcceptable) c.AbortWithStatus(http.StatusNotAcceptable)