added last online and session expiring
parent
042bc9394a
commit
26f99754e0
|
@ -97,6 +97,7 @@ type AllUsers struct {
|
|||
Avatar string
|
||||
Username string
|
||||
ConnectionStatus uint8
|
||||
LastOnline time.Time
|
||||
}
|
||||
|
||||
type UserData struct {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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})
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue