mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
35 lines
724 B
Go
35 lines
724 B
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo-contrib/session"
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/rjNemo/rentease/constant"
|
|
)
|
|
|
|
func MakeAuthMiddleware() echo.MiddlewareFunc {
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
if c.Request().RequestURI == constant.RouteLogin {
|
|
return next(c)
|
|
}
|
|
|
|
s, err := readSession(c)
|
|
if s != "bar" || err != nil {
|
|
return c.Redirect(http.StatusSeeOther, constant.RouteLogin)
|
|
}
|
|
return next(c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func readSession(c echo.Context) (string, error) {
|
|
sess, err := session.Get(sessionName, c)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return fmt.Sprintf("%s", sess.Values["foo"]), nil
|
|
}
|