From 10464f402e2befc2c5007e8486d4ac0bc95b7974 Mon Sep 17 00:00:00 2001 From: "Grail Finder (aider)" Date: Fri, 2 May 2025 09:54:32 +0300 Subject: [PATCH] feat: add login middleware for protected routes --- handlers/middleware.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/handlers/middleware.go b/handlers/middleware.go index 81ae497..4b8ce60 100644 --- a/handlers/middleware.go +++ b/handlers/middleware.go @@ -16,7 +16,16 @@ var ( memcache cache.Cache ) -// add middleware to login http requests; ai! +// RequireLogin redirects unauthenticated users to the login page +func RequireLogin(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Context().Value("username") == nil { + http.Redirect(w, r, "/login", http.StatusFound) + return + } + next.ServeHTTP(w, r) + }) +} func GetSession(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {