ovpn-certman/middleware/requirelogin.go

23 lines
595 B
Go
Raw Normal View History

2018-01-26 08:43:53 +01:00
package middleware
import (
"net/http"
2018-01-29 09:18:19 +01:00
"git.klink.asia/paul/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) {
if username := sessions.GetUserEmail(req); username == "" {
http.Redirect(w, req, "/login", http.StatusFound)
}
2018-01-26 08:43:53 +01:00
next.ServeHTTP(w, req)
}
return http.HandlerFunc(fn)
2018-01-26 08:43:53 +01:00
}
}