feat: add login middleware for protected routes

This commit is contained in:
Grail Finder (aider)
2025-05-02 09:54:32 +03:00
parent 555c937fce
commit 10464f402e

View File

@ -16,7 +16,16 @@ var (
memcache cache.Cache 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 { func GetSession(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {