gitstack

grindlemire/gothem-stack code browser

613 B Go 21 lines 2025-11-29 · 98bbe10 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package auth

import (
	"github.com/labstack/echo/v4"
	"github.com/pkg/errors"
)

// Middleware is a simple middleware that checks the request for authentication
func Middleware() echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) (err error) {
			// obviously this is not real authentication and is just illustrative of what you can do here
			username := c.Request().URL.Query().Get("username")
			if username == "reject" {
				return echo.ErrUnauthorized.SetInternal(errors.Errorf("user %s is not authorized", username))
			}

			return next(c)
		}
	}
}