aboutsummaryrefslogtreecommitdiff
path: root/middleware/user.go
blob: f4eb12aa782ef94064f4efe84c026de793f3f2e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package middleware

import (
	"context"
	"net/http"

	"gitrepo.ru/neonxp/gorum/contextlib"
	"go.neonxp.ru/mux"
	"go.neonxp.ru/mux/middleware/session"
)

func UserMiddleware() mux.Middleware {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			session := session.FromRequest(r)
			user := (*session)["user"]
			if user == nil {
				h.ServeHTTP(w, r)
				return
			}
			h.ServeHTTP(w, r.WithContext(
				context.WithValue(r.Context(), contextlib.UserKey, user),
			))
		})
	}
}