chore(webui): refactor login and session middleware handling
This commit is contained in:
parent
1292d189cf
commit
7885b19b54
2 changed files with 9 additions and 9 deletions
|
@ -88,9 +88,9 @@ func handleLoginPOST(db *database.DB) http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func loginMiddleware(db *database.DB) func(http.Handler) http.Handler {
|
func loginMiddleware(db *database.DB, requireSession func(http.Handler) http.Handler) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return requireSession(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Cache-Control", "no-store, no-cache")
|
w.Header().Set("Cache-Control", "no-store, no-cache")
|
||||||
session := r.Context().Value(model.SessionContextKey{})
|
session := r.Context().Value(model.SessionContextKey{})
|
||||||
if session == nil {
|
if session == nil {
|
||||||
|
@ -110,6 +110,6 @@ func loginMiddleware(db *database.DB) func(http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
ctx := context.WithValue(r.Context(), model.AccountContextKey{}, account)
|
ctx := context.WithValue(r.Context(), model.AccountContextKey{}, account)
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,12 +10,12 @@ func addRoutes(
|
||||||
mux *http.ServeMux,
|
mux *http.ServeMux,
|
||||||
db *database.DB,
|
db *database.DB,
|
||||||
) {
|
) {
|
||||||
session := sessionsMiddleware(db)
|
requireSession := sessionsMiddleware(db)
|
||||||
requireLogin := loginMiddleware(db)
|
requireLogin := loginMiddleware(db, requireSession)
|
||||||
mux.Handle("GET /healthz", handleHealthz())
|
mux.Handle("GET /healthz", handleHealthz())
|
||||||
mux.Handle("GET /login", session(handleLoginGET()))
|
mux.Handle("GET /login", requireSession(handleLoginGET()))
|
||||||
mux.Handle("POST /login", session(handleLoginPOST(db)))
|
mux.Handle("POST /login", requireSession(handleLoginPOST(db)))
|
||||||
mux.Handle("GET /logout", session(requireLogin(handleLogoutGET(db))))
|
mux.Handle("GET /logout", requireLogin(handleLogoutGET(db)))
|
||||||
mux.Handle("GET /static/", cache(http.FileServer(http.FS(staticFS))))
|
mux.Handle("GET /static/", cache(http.FileServer(http.FS(staticFS))))
|
||||||
mux.Handle("GET /", session(requireLogin(handleIndexGET())))
|
mux.Handle("GET /", requireLogin(handleIndexGET()))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue