diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-08-04 20:33:37 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-08-04 20:33:37 +0900 |
| commit | 0f0324b396f3eab53606c8f770d26337dd0e291a (patch) | |
| tree | 0a2afd4701535b11c81fb3908d8c241eaaeb7d21 /backend/admin/handlers.go | |
| parent | d87507918f33b289ac4fc4dece8a54fa3aa34923 (diff) | |
| download | phperkaigi-2025-albatross-0f0324b396f3eab53606c8f770d26337dd0e291a.tar.gz phperkaigi-2025-albatross-0f0324b396f3eab53606c8f770d26337dd0e291a.tar.zst phperkaigi-2025-albatross-0f0324b396f3eab53606c8f770d26337dd0e291a.zip | |
feat: authenticate users in admin pages
Diffstat (limited to 'backend/admin/handlers.go')
| -rw-r--r-- | backend/admin/handlers.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/backend/admin/handlers.go b/backend/admin/handlers.go index f81856c..14523e6 100644 --- a/backend/admin/handlers.go +++ b/backend/admin/handlers.go @@ -10,6 +10,7 @@ import ( "github.com/jackc/pgx/v5/pgtype" "github.com/labstack/echo/v4" + "github.com/nsfisis/iosdc-japan-2024-albatross/backend/auth" "github.com/nsfisis/iosdc-japan-2024-albatross/backend/db" ) @@ -31,8 +32,28 @@ func NewAdminHandler(q *db.Queries, hubs GameHubsInterface) *AdminHandler { } } +func newAdminMiddleware() echo.MiddlewareFunc { + return func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + jwt, err := c.Cookie("albatross_token") + if err != nil { + return c.Redirect(http.StatusSeeOther, "/login") + } + claims, err := auth.ParseJWT(jwt.Value) + if err != nil { + return c.Redirect(http.StatusSeeOther, "/login") + } + if !claims.IsAdmin { + return echo.NewHTTPError(http.StatusForbidden) + } + return next(c) + } + } +} + func (h *AdminHandler) RegisterHandlers(g *echo.Group) { g.Use(newAssetsMiddleware()) + g.Use(newAdminMiddleware()) g.GET("/dashboard", h.getDashboard) g.GET("/users", h.getUsers) |
