an updated and hopefully faster version of the ST Toolbox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

36 lines
837 B

package middleware
import (
"context"
"marmic/servicetrade-toolbox/internal/api"
"net/http"
)
var SessionStore = api.NewSessionStore()
func AuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("PHPSESSID")
if err != nil {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
sessionID := cookie.Value
session, exists := SessionStore.Get(sessionID)
if !exists {
session = api.NewSession()
session.Cookie = "PHPSESSID=" + sessionID
if err := session.ValidateSession(); err != nil {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
SessionStore.Set(sessionID, session)
}
ctx := context.WithValue(r.Context(), "session", session)
next.ServeHTTP(w, r.WithContext(ctx))
})
}