ovpn-certman/middleware/requirelogin.go

24 lines
608 B
Go
Raw Normal View History

2018-01-26 08:43:53 +01:00
package middleware
import (
"net/http"
"github.com/zom-bi/ovpn-certman/services"
2018-01-26 08:43:53 +01:00
)
2018-01-29 09:18:19 +01:00
// RequireLogin is a middleware that checks for a username in the active
// session, and redirects to `/login` if no username was found.
func RequireLogin(sessions *services.Sessions) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, req *http.Request) {
2018-02-01 09:31:06 +01:00
if username := sessions.GetUsername(req); username == "" {
http.Redirect(w, req, "/login", http.StatusFound)
2019-05-15 19:08:24 +02:00
return
}
2018-01-26 08:43:53 +01:00
next.ServeHTTP(w, req)
}
return http.HandlerFunc(fn)
2018-01-26 08:43:53 +01:00
}
}