aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-08-04 20:33:37 +0900
committernsfisis <nsfisis@gmail.com>2024-08-04 20:33:37 +0900
commit0f0324b396f3eab53606c8f770d26337dd0e291a (patch)
tree0a2afd4701535b11c81fb3908d8c241eaaeb7d21 /backend
parentd87507918f33b289ac4fc4dece8a54fa3aa34923 (diff)
downloadiosdc-japan-2024-albatross-0f0324b396f3eab53606c8f770d26337dd0e291a.tar.gz
iosdc-japan-2024-albatross-0f0324b396f3eab53606c8f770d26337dd0e291a.tar.zst
iosdc-japan-2024-albatross-0f0324b396f3eab53606c8f770d26337dd0e291a.zip
feat: authenticate users in admin pages
Diffstat (limited to 'backend')
-rw-r--r--backend/admin/handlers.go21
-rw-r--r--backend/main.go8
2 files changed, 26 insertions, 3 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)
diff --git a/backend/main.go b/backend/main.go
index 2d38ee5..e2e4bbd 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -83,9 +83,11 @@ func main() {
adminGroup := e.Group("/admin")
adminHandler.RegisterHandlers(adminGroup)
- // For local dev:
- // This is never used in production because the reverse proxy sends /logout
- // to the app server.
+ // For local dev: This is never used in production because the reverse
+ // proxy sends /login and /logout to the app server.
+ e.GET("/login", func(c echo.Context) error {
+ return c.Redirect(http.StatusPermanentRedirect, "http://localhost:5173/login")
+ })
e.POST("/logout", func(c echo.Context) error {
return c.Redirect(http.StatusPermanentRedirect, "http://localhost:5173/logout")
})