ovpn-certman/middleware/requirelogin.go
2018-01-29 09:18:19 +01:00

20 lines
522 B
Go

package middleware
import (
"net/http"
"git.klink.asia/paul/certman/services"
)
// RequireLogin is a middleware that checks for a username in the active
// session, and redirects to `/login` if no username was found.
func RequireLogin(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, req *http.Request) {
if username := services.SessionStore.GetUserEmail(req); username == "" {
http.Redirect(w, req, "/login", http.StatusFound)
}
next.ServeHTTP(w, req)
}
return http.HandlerFunc(fn)
}