2025-03-01 23:22:18 +07:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
2025-07-31 20:55:47 -04:00
|
|
|
"Adekabang/garage-webui/utils"
|
2025-03-01 23:22:18 +07:00
|
|
|
"errors"
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func AuthMiddleware(next http.Handler) http.Handler {
|
|
|
|
|
authData := utils.GetEnv("AUTH_USER_PASS", "")
|
|
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
auth := utils.Session.Get(r, "authenticated")
|
|
|
|
|
|
|
|
|
|
if authData == "" {
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if auth == nil || !auth.(bool) {
|
|
|
|
|
utils.ResponseErrorStatus(w, errors.New("unauthorized"), http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|