added last online and session expiring

main
alex 2023-06-11 20:24:29 +02:00
parent 042bc9394a
commit 26f99754e0
5 changed files with 29 additions and 13 deletions

View File

@ -97,6 +97,7 @@ type AllUsers struct {
Avatar string
Username string
ConnectionStatus uint8
LastOnline time.Time
}
type UserData struct {

View File

@ -5,12 +5,13 @@ import (
)
type User struct {
Id string
Avatar string
Username string
Email string
Password string
CreatedAt time.Time
Id string
Avatar string
Username string
Email string
Password string
LastOnline time.Time
CreatedAt time.Time
}
type UserSession struct {

View File

@ -66,6 +66,8 @@ func UserLogin(c *fiber.Ctx) error {
UserAgent: string(c.Context().UserAgent()),
ExpiresAt: utils.GetSessionExpiresAtTime()})
log.Warn().Msgf("create session %v", session)
return c.JSON(structs.UserLoginResponse{Session: session})
}

View File

@ -5,6 +5,7 @@ import (
"janex/admin-dashboard-backend/modules/database"
"janex/admin-dashboard-backend/modules/structs"
"janex/admin-dashboard-backend/modules/utils"
"time"
)
func BroadcastMessage(sendSocketMessage structs.SendSocketMessage) {
@ -22,16 +23,22 @@ func BroadcastMessageExceptUserSessionId(ignoreUserSessionId string, sendSocketM
}
func UpdateConnectedUsers(userId string) {
var user structs.User
database.DB.First(&user, "id = ?", userId)
BroadcastMessage(structs.SendSocketMessage{
Cmd: utils.SentCmdUpdateConnectedUsers,
Body: struct {
WebSocketUsersCount int
UserId string
ConnectionStatus uint8
LastOnline time.Time
}{
WebSocketUsersCount: len(cache.GetSocketClients()),
UserId: userId,
ConnectionStatus: isUserGenerallyConnected(userId),
LastOnline: user.LastOnline,
},
})
}
@ -120,6 +127,7 @@ func GetAllUsers() []structs.AllUsers {
Avatar: user.Avatar,
Username: user.Username,
ConnectionStatus: isUserGenerallyConnected(user.Id),
LastOnline: user.LastOnline,
})
}

View File

@ -44,9 +44,9 @@ func RunHub() {
// check that user session is not expired
var userSession structs.UserSession
database.DB.First(&userSession, "user_id = ?", userId)
database.DB.First(&userSession, "id = ?", sessionId)
if time.Now().After(userSession.ExpiresAt) {
if userSession.Id != "" && time.Now().After(userSession.ExpiresAt) {
newSocketClient.SendUnauthorizedCloseMessage()
database.DB.Delete(&structs.UserSession{}, "id = ?", sessionId)
continue
@ -202,13 +202,17 @@ func RunHub() {
case connection := <-unregister:
cache.DeleteClientByConn(connection)
userId := connection.Locals("userId").(string)
sessionid := connection.Locals("sessionId").(string)
if connection.Locals("userId") != nil && connection.Locals("sessionId") != nil {
userId := connection.Locals("userId").(string)
sessionId := connection.Locals("sessionId").(string)
if userId != "" && sessionid != "" {
socketclients.UpdateUserSessionsForUser(connection.Locals("userId").(string), connection.Locals("sessionId").(string))
database.DB.Model(&structs.User{}).Where("id = ?", userId).Updates(structs.User{
LastOnline: time.Now(),
})
socketclients.UpdateConnectedUsers(connection.Locals("userId").(string))
socketclients.UpdateUserSessionsForUser(userId, sessionId)
socketclients.UpdateConnectedUsers(userId)
}
}
}