From 188d3596a22f93ac297bda15bc1245715ed169e9 Mon Sep 17 00:00:00 2001 From: sertraline <36751007+sertraline@users.noreply.github.com> Date: Tue, 11 Jul 2023 02:01:36 +0300 Subject: [PATCH] Update README.md --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index d447531..996e9b1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,31 @@ # go-session-redis Simple HTTP session management + +## Example middleware (Chi router) +```go +func SessionAuthenticator(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + s, err := session.SessionManager.GetOrCreateSession(w, r) + if err != nil { + http.Error(w, err.Error(), http.StatusUnauthorized) + return + } + + id := s.Get("id") + if id == "" || id == nil { + http.Error(w, "Not authenticated", http.StatusUnauthorized) + return + } + + err = s.UpdateTTL() + if err != nil { + http.Error(w, err.Error(), http.StatusBadGateway) + return + } + + // Session is valid + next.ServeHTTP(w, r) + }) +} + +```