udemy-go-web-2/cmd/web/middleware.go

19 lines
398 B
Go
Raw Normal View History

package main
import (
"net/http"
)
func SessionLoad(next http.Handler) http.Handler {
return session.LoadAndSave(next)
}
func (app *application) Auth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !app.Session.Exists(r.Context(), "userID") {
http.Redirect(w, r, "/login", http.StatusSeeOther)
}
next.ServeHTTP(w, r)
})
}