diff --git a/handlers/middleware.go b/handlers/middleware.go index e66c99e..6f856c4 100644 --- a/handlers/middleware.go +++ b/handlers/middleware.go @@ -20,6 +20,15 @@ func MiddlewareSession(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { sess, _ := session.Get(setting.SessionCookieName(), c) + if sess == nil { + return renderErrorPage( + c, + http.StatusUnauthorized, + http.StatusText(http.StatusUnauthorized)+" you need to log in again", + "you need to log in again", + ) + } + var username string // uname, ok := sess.Values["username"].(string) @@ -39,6 +48,8 @@ func MiddlewareSession(next echo.HandlerFunc) echo.HandlerFunc { http.SameSiteStrictMode, ) + sess.Values["username"] = username + c.Set("sess", sess) var u moduser.User @@ -66,7 +77,7 @@ func MiddlewareSession(next echo.HandlerFunc) echo.HandlerFunc { c.Set("sessUsr", u) if err := sess.Save(c.Request(), c.Response()); err != nil { - c.Logger().Error("Failed to save session", "module", "middleware") + log.Error("Failed to save session", "module", "middleware") return renderErrorPage( c, @@ -79,21 +90,19 @@ func MiddlewareSession(next echo.HandlerFunc) echo.HandlerFunc { return next(c) } + log.Warn("Could not get username from the cookie") + if !sess.IsNew { - c.Logger().Errorf("%d - %s", http.StatusUnauthorized, "you need to log in") + log.Errorf("%d - %s", http.StatusUnauthorized, "you need to re-login") return c.Redirect(http.StatusTemporaryRedirect, "/signin") } - // return renderErrorPage( - // c, - // http.StatusUnauthorized, - // http.StatusText(http.StatusUnauthorized), - // ErrNoSession.Error(), - // ) - - c.Logger().Warn("Could not get username from the cookie") - - return next(c) + return renderErrorPage( + c, + http.StatusUnauthorized, + http.StatusText(http.StatusUnauthorized), + ErrNoSession.Error(), + ) } }