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-29 16:52:59 +01:00
|
|
|
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 == "" {
|
2018-01-29 16:52:59 +01:00
|
|
|
http.Redirect(w, req, "/login", http.StatusFound)
|
|
|
|
}
|
2018-01-26 08:43:53 +01:00
|
|
|
|
2018-01-29 16:52:59 +01:00
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
2018-01-26 08:43:53 +01:00
|
|
|
}
|
|
|
|
}
|