ovpn-certman/middleware/requirelogin.go

21 lines
522 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.
2018-01-26 08:43:53 +01:00
func RequireLogin(next http.Handler) http.Handler {
2018-01-29 09:18:19 +01:00
fn := func(w http.ResponseWriter, req *http.Request) {
if username := services.SessionStore.GetUserEmail(req); username == "" {
http.Redirect(w, req, "/login", http.StatusFound)
}
2018-01-26 08:43:53 +01:00
2018-01-29 09:18:19 +01:00
next.ServeHTTP(w, req)
2018-01-26 08:43:53 +01:00
}
return http.HandlerFunc(fn)
}