sessions: Can store values

This commit is contained in:
nemunaire 2022-09-04 11:12:37 +02:00
commit 4baa665693
3 changed files with 50 additions and 14 deletions

9
api.go
View file

@ -69,12 +69,13 @@ func adminRestricted(u *User, c *gin.Context) bool {
func authMiddleware(access ...func(*User, *gin.Context) bool) gin.HandlerFunc {
return func(c *gin.Context) {
var user *User = nil
var session *Session = nil
if cookie, err := c.Request.Cookie("auth"); err == nil {
if sessionid, err := base64.StdEncoding.DecodeString(cookie.Value); err != nil {
eraseCookie(c)
c.AbortWithStatusJSON(http.StatusNotAcceptable, gin.H{"errmsg": err.Error()})
return
} else if session, err := getSession(sessionid); err != nil {
} else if session, err = getSession(sessionid); err != nil {
eraseCookie(c)
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"errmsg": err.Error()})
return
@ -97,9 +98,15 @@ func authMiddleware(access ...func(*User, *gin.Context) bool) gin.HandlerFunc {
}
// Retrieve corresponding user
c.Set("Session", session)
c.Set("LoggedUser", user)
// We are now ready to continue
c.Next()
// On return, check if the session has changed
if session != nil && session.HasChanged() {
session.Update()
}
}
}