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 (
"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)
}

View File

@ -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,
}

View File

@ -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
}

View File

@ -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)