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 Avatar string
Username string Username string
ConnectionStatus uint8 ConnectionStatus uint8
LastOnline time.Time
} }
type UserData struct { type UserData struct {

View File

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

View File

@ -66,6 +66,8 @@ func UserLogin(c *fiber.Ctx) error {
UserAgent: string(c.Context().UserAgent()), UserAgent: string(c.Context().UserAgent()),
ExpiresAt: utils.GetSessionExpiresAtTime()}) ExpiresAt: utils.GetSessionExpiresAtTime()})
log.Warn().Msgf("create session %v", session)
return c.JSON(structs.UserLoginResponse{Session: 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/database"
"janex/admin-dashboard-backend/modules/structs" "janex/admin-dashboard-backend/modules/structs"
"janex/admin-dashboard-backend/modules/utils" "janex/admin-dashboard-backend/modules/utils"
"time"
) )
func BroadcastMessage(sendSocketMessage structs.SendSocketMessage) { func BroadcastMessage(sendSocketMessage structs.SendSocketMessage) {
@ -22,16 +23,22 @@ func BroadcastMessageExceptUserSessionId(ignoreUserSessionId string, sendSocketM
} }
func UpdateConnectedUsers(userId string) { func UpdateConnectedUsers(userId string) {
var user structs.User
database.DB.First(&user, "id = ?", userId)
BroadcastMessage(structs.SendSocketMessage{ BroadcastMessage(structs.SendSocketMessage{
Cmd: utils.SentCmdUpdateConnectedUsers, Cmd: utils.SentCmdUpdateConnectedUsers,
Body: struct { Body: struct {
WebSocketUsersCount int WebSocketUsersCount int
UserId string UserId string
ConnectionStatus uint8 ConnectionStatus uint8
LastOnline time.Time
}{ }{
WebSocketUsersCount: len(cache.GetSocketClients()), WebSocketUsersCount: len(cache.GetSocketClients()),
UserId: userId, UserId: userId,
ConnectionStatus: isUserGenerallyConnected(userId), ConnectionStatus: isUserGenerallyConnected(userId),
LastOnline: user.LastOnline,
}, },
}) })
} }
@ -120,6 +127,7 @@ func GetAllUsers() []structs.AllUsers {
Avatar: user.Avatar, Avatar: user.Avatar,
Username: user.Username, Username: user.Username,
ConnectionStatus: isUserGenerallyConnected(user.Id), ConnectionStatus: isUserGenerallyConnected(user.Id),
LastOnline: user.LastOnline,
}) })
} }

View File

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