ensure user in session exists

This commit is contained in:
sentriz
2019-04-25 14:40:35 +01:00
parent 4e48fe8a90
commit 2d7e2e379b

View File

@@ -123,6 +123,14 @@ func (c *Controller) WithUserSession(next http.HandlerFunc) http.HandlerFunc {
}
// take username from sesion and add the user row
user := c.GetUserFromName(username)
if user.ID == 0 {
// the username in the client's session no longer relates to a
// user in the database (maybe the user was deleted)
session.Options.MaxAge = -1
session.Save(r, w)
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
return
}
withUser := context.WithValue(r.Context(), "user", user)
next.ServeHTTP(w, r.WithContext(withUser))
}