package server

import (
	"net/http"

	"github.com/labstack/echo/v4"

	"github.com/rjNemo/rentease/internal/constant"
	"github.com/rjNemo/rentease/internal/service/auth"
)

func MakeAuthMiddleware(as *auth.Service) echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			if c.Request().RequestURI == constant.RouteLogin {
				return next(c)
			}

			if !as.Authenticated(c) {
				return c.Redirect(http.StatusSeeOther, constant.RouteLogin)
			}
			return next(c)
		}
	}
}
