go(sessionMiddleware): render err page on 401
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
fa1253a675
commit
fcea85e54b
@ -20,6 +20,15 @@ func MiddlewareSession(next echo.HandlerFunc) echo.HandlerFunc {
|
|||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
sess, _ := session.Get(setting.SessionCookieName(), c)
|
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
|
var username string
|
||||||
|
|
||||||
// uname, ok := sess.Values["username"].(string)
|
// uname, ok := sess.Values["username"].(string)
|
||||||
@ -39,6 +48,8 @@ func MiddlewareSession(next echo.HandlerFunc) echo.HandlerFunc {
|
|||||||
http.SameSiteStrictMode,
|
http.SameSiteStrictMode,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
sess.Values["username"] = username
|
||||||
|
|
||||||
c.Set("sess", sess)
|
c.Set("sess", sess)
|
||||||
|
|
||||||
var u moduser.User
|
var u moduser.User
|
||||||
@ -66,7 +77,7 @@ func MiddlewareSession(next echo.HandlerFunc) echo.HandlerFunc {
|
|||||||
c.Set("sessUsr", u)
|
c.Set("sessUsr", u)
|
||||||
|
|
||||||
if err := sess.Save(c.Request(), c.Response()); err != nil {
|
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(
|
return renderErrorPage(
|
||||||
c,
|
c,
|
||||||
@ -79,21 +90,19 @@ func MiddlewareSession(next echo.HandlerFunc) echo.HandlerFunc {
|
|||||||
return next(c)
|
return next(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Warn("Could not get username from the cookie")
|
||||||
|
|
||||||
if !sess.IsNew {
|
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 c.Redirect(http.StatusTemporaryRedirect, "/signin")
|
||||||
}
|
}
|
||||||
|
|
||||||
// return renderErrorPage(
|
return renderErrorPage(
|
||||||
// c,
|
c,
|
||||||
// http.StatusUnauthorized,
|
http.StatusUnauthorized,
|
||||||
// http.StatusText(http.StatusUnauthorized),
|
http.StatusText(http.StatusUnauthorized),
|
||||||
// ErrNoSession.Error(),
|
ErrNoSession.Error(),
|
||||||
// )
|
)
|
||||||
|
|
||||||
c.Logger().Warn("Could not get username from the cookie")
|
|
||||||
|
|
||||||
return next(c)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user