package web import ( "errors" "net/http" "time" "github.com/labstack/echo/v4" ) var sessions map[string]time.Time func init() { sessions = make(map[string]time.Time) } func checkAuth(token string) error { for sToken, expiree := range sessions { // Expire old sessions if time.Now().After(expiree) { delete(sessions, sToken) continue } // Check for valid session of token if token == sToken { return nil } } return errors.New("this token is not associated with a valid session") } func withAuth(handler echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { session, err := c.Cookie("session") if err != nil || checkAuth(session.Value) != nil { return c.Redirect(http.StatusSeeOther, "/") } return handler(c) } }