Moved auth for admin to middleware
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kolaente 2019-09-03 20:41:14 +02:00
parent a4955be200
commit 11acd8e277
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 10 additions and 14 deletions

View File

@ -34,11 +34,6 @@ func (h *Handler) ReadAll(c echo.Context) error {
}
func (h *Handler) Create(c echo.Context) error {
if !isLoggedIn(c) {
return echo.NewHTTPError(http.StatusForbidden, "Login first.")
}
str := h.str()
if err := c.Bind(str); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
@ -53,10 +48,6 @@ func (h *Handler) Create(c echo.Context) error {
}
func (h *Handler) Delete(c echo.Context) error {
if !isLoggedIn(c) {
return echo.NewHTTPError(http.StatusForbidden, "Login first.")
}
str := h.str()
if err := c.Bind(str); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
@ -71,10 +62,6 @@ func (h *Handler) Delete(c echo.Context) error {
}
func (h *Handler) Update(c echo.Context) error {
if !isLoggedIn(c) {
return echo.NewHTTPError(http.StatusForbidden, "Login first.")
}
str := h.str()
if err := c.Bind(str); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())

View File

@ -8,6 +8,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"html/template"
"net/http"
)
func NewEcho() *echo.Echo {
@ -61,7 +62,15 @@ func RegisterRoutes(e *echo.Echo) {
}
e.GET("/list", handler.ReadAll)
// Routes with auth -> TODO: build a middleware which checks this
// Routes with auth
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if !isLoggedIn(c) {
return echo.NewHTTPError(http.StatusForbidden, "Login first.")
}
return next(c)
}
})
e.POST("/update", handler.Update)
e.POST("/delete", handler.Delete)
e.POST("/add", handler.Create)