setting last used and expires time for user sessions
parent
98bf8197ed
commit
042bc9394a
|
@ -11,6 +11,11 @@
|
||||||
"parameterName": "kiste",
|
"parameterName": "kiste",
|
||||||
"type": "number",
|
"type": "number",
|
||||||
"displayName": "Nummer der Kiste"
|
"displayName": "Nummer der Kiste"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"parameterName": "kiste2",
|
||||||
|
"type": "textarea",
|
||||||
|
"displayName": "Nummer der zweiten Kiste yooo"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"tasks": [
|
"tasks": [
|
||||||
|
@ -31,6 +36,12 @@
|
||||||
"type": "number",
|
"type": "number",
|
||||||
"displayName": "Nummer der Kiste",
|
"displayName": "Nummer der Kiste",
|
||||||
"global": true
|
"global": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"parameterName": "kiste2",
|
||||||
|
"type": "textarea",
|
||||||
|
"displayName": "Nummer der zweiten Kiste lul",
|
||||||
|
"global": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
|
@ -18,7 +18,8 @@ const (
|
||||||
|
|
||||||
MaxAvatarSize = 5 * 1024 * 1024 // 5 MB
|
MaxAvatarSize = 5 * 1024 * 1024 // 5 MB
|
||||||
|
|
||||||
GroupTaskLockedTime = 3 // seconds - need to be equal with web
|
GroupTaskLockedTime = 3 // seconds - need to be equal with web
|
||||||
|
SessionExpiresAtTime = 7 * 24 * 60 * 60 // 1 week
|
||||||
)
|
)
|
||||||
|
|
||||||
// commands sent to web clients
|
// commands sent to web clients
|
||||||
|
|
|
@ -2,6 +2,7 @@ package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
@ -21,3 +22,7 @@ func MarshalJson(v any) string {
|
||||||
|
|
||||||
return string(json)
|
return string(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetSessionExpiresAtTime() time.Time {
|
||||||
|
return time.Now().Add(time.Second * SessionExpiresAtTime)
|
||||||
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ func UserLogin(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := utils.ValidateStruct(body); err != nil {
|
if err := utils.ValidateStruct(body); err != nil {
|
||||||
log.Error().Msgf("Failed to validate body, err: %s", err)
|
log.Error().Msgf("Failed to validate body, err: %v", err)
|
||||||
return c.SendStatus(fiber.StatusBadRequest)
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,8 @@ func UserLogin(c *fiber.Ctx) error {
|
||||||
Id: session,
|
Id: session,
|
||||||
IdForDeletion: uuid.V4(),
|
IdForDeletion: uuid.V4(),
|
||||||
UserId: user.Id,
|
UserId: user.Id,
|
||||||
UserAgent: string(c.Context().UserAgent())})
|
UserAgent: string(c.Context().UserAgent()),
|
||||||
|
ExpiresAt: utils.GetSessionExpiresAtTime()})
|
||||||
|
|
||||||
return c.JSON(structs.UserLoginResponse{Session: session})
|
return c.JSON(structs.UserLoginResponse{Session: session})
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,24 @@ func RunHub() {
|
||||||
log.Debug().Msgf("clients: %d", len(cache.GetSocketClients()))
|
log.Debug().Msgf("clients: %d", len(cache.GetSocketClients()))
|
||||||
log.Debug().Msgf("REGISTER CLIENT: %s", sessionId)
|
log.Debug().Msgf("REGISTER CLIENT: %s", sessionId)
|
||||||
|
|
||||||
|
// check that user session is not expired
|
||||||
|
var userSession structs.UserSession
|
||||||
|
|
||||||
|
database.DB.First(&userSession, "user_id = ?", userId)
|
||||||
|
|
||||||
|
if time.Now().After(userSession.ExpiresAt) {
|
||||||
|
newSocketClient.SendUnauthorizedCloseMessage()
|
||||||
|
database.DB.Delete(&structs.UserSession{}, "id = ?", sessionId)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// update session last used time
|
||||||
|
database.DB.Model(&structs.UserSession{}).Where("id = ?", sessionId).Updates(structs.UserSession{
|
||||||
|
LastUsed: time.Now(),
|
||||||
|
ExpiresAt: utils.GetSessionExpiresAtTime(),
|
||||||
|
})
|
||||||
|
|
||||||
|
// init data message
|
||||||
var user structs.User
|
var user structs.User
|
||||||
|
|
||||||
database.DB.First(&user, "id = ?", userId)
|
database.DB.First(&user, "id = ?", userId)
|
||||||
|
@ -184,9 +202,14 @@ func RunHub() {
|
||||||
case connection := <-unregister:
|
case connection := <-unregister:
|
||||||
cache.DeleteClientByConn(connection)
|
cache.DeleteClientByConn(connection)
|
||||||
|
|
||||||
socketclients.UpdateUserSessionsForUser(connection.Locals("userId").(string), connection.Locals("sessionId").(string))
|
userId := connection.Locals("userId").(string)
|
||||||
|
sessionid := connection.Locals("sessionId").(string)
|
||||||
|
|
||||||
socketclients.UpdateConnectedUsers(connection.Locals("userId").(string))
|
if userId != "" && sessionid != "" {
|
||||||
|
socketclients.UpdateUserSessionsForUser(connection.Locals("userId").(string), connection.Locals("sessionId").(string))
|
||||||
|
|
||||||
|
socketclients.UpdateConnectedUsers(connection.Locals("userId").(string))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue