go(sessionMiddleware): render err page on 401
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2023-09-04 15:31:11 +02:00
parent fa1253a675
commit fcea85e54b
Signed by: wanderer
SSH Key Fingerprint: SHA256:MdCZyJ2sHLltrLBp0xQO0O1qTW9BT/xl5nXkDvhlMCI

View File

@ -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(),
)
}
}