session sign out

main
alex 2023-05-15 22:03:52 +02:00
parent 61d6c5ee0f
commit ccda8d607e
8 changed files with 47 additions and 11 deletions

View File

@ -97,6 +97,7 @@ type UserData struct {
} }
type UserSessionSocket struct { type UserSessionSocket struct {
IdForDeletion string
UserAgent string UserAgent string
ConnectionStatus uint8 ConnectionStatus uint8
LastUsed time.Time LastUsed time.Time

View File

@ -13,7 +13,8 @@ type User struct {
} }
type UserSession struct { type UserSession struct {
Id string Id string // user session which he use to connect to the websocket and api server
IdForDeletion string // this id is needed to sign out a session from website
UserId string UserId string
UserAgent string UserAgent string
LastUsed time.Time LastUsed time.Time
@ -33,3 +34,7 @@ type UserResponse struct {
Username string Username string
Email string Email string
} }
type UserSignOutSessionRequest struct {
SessionId string
}

View File

@ -9,6 +9,7 @@ const (
MaxPassword = 64 MaxPassword = 64
LenHeaderXAuthorization = 36 LenHeaderXAuthorization = 36
lenHeaderXAuthorization = "36"
LenUserId = 36 LenUserId = 36
HeaderXAuthorization = "X-Authorization" HeaderXAuthorization = "X-Authorization"
@ -39,5 +40,6 @@ var (
generalRules = map[string]string{ generalRules = map[string]string{
"Username": "required,min=" + minUsername + ",max=" + maxUsername, "Username": "required,min=" + minUsername + ",max=" + maxUsername,
"Password": "required", // length is checked later because sent in base64 "Password": "required", // length is checked later because sent in base64
"SessionId": "required,len" + lenHeaderXAuthorization,
} }
) )

View File

@ -30,5 +30,5 @@ func ValidateStruct(event interface{}) []*ErrorResponse {
} }
func ValidatorInit() { func ValidatorInit() {
Validate.RegisterStructValidationMapRules(generalRules, structs.UserLoginRequest{}) Validate.RegisterStructValidationMapRules(generalRules, structs.UserLoginRequest{}, structs.UserSignOutSessionRequest{})
} }

View File

@ -11,6 +11,7 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/savsgio/gotils/uuid"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
@ -60,6 +61,7 @@ func UserLogin(c *fiber.Ctx) error {
database.DB.Create(&structs.UserSession{ database.DB.Create(&structs.UserSession{
Id: session, Id: session,
IdForDeletion: uuid.V4(),
UserId: user.Id, UserId: user.Id,
UserAgent: string(c.Context().UserAgent())}) UserAgent: string(c.Context().UserAgent())})

View File

@ -0,0 +1,24 @@
package user
import (
"janex/admin-dashboard-backend/modules/structs"
"janex/admin-dashboard-backend/modules/utils"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
)
func SignOutSession(c *fiber.Ctx) error {
var params structs.UserSignOutSessionRequest
if err := c.ParamsParser(&params); err != nil {
log.Error().Msg("Failed to parse params, err: " + err.Error())
return c.SendStatus(fiber.StatusBadRequest)
}
log.Debug().Msgf("params %s", params.SessionId)
log.Debug().Msgf("userId %s", utils.GetXAuhorizationHeader(c))
return c.SendStatus(fiber.StatusOK)
}

View File

@ -15,6 +15,7 @@ func SetupRoutes(app *fiber.App) {
u := v1.Group("/user") u := v1.Group("/user")
u.Post("/auth/login", user.UserLogin) u.Post("/auth/login", user.UserLogin)
u.Delete("/auth/logout", user.UserLogout) u.Delete("/auth/logout", user.UserLogout)
u.Delete("/session/:sessionId", user.SignOutSession)
} }
func userSessionValidation(c *fiber.Ctx) error { func userSessionValidation(c *fiber.Ctx) error {

View File

@ -49,6 +49,7 @@ func GetUserSessions(userId string) []structs.UserSessionSocket {
for _, userSession := range userSessions { for _, userSession := range userSessions {
userSessionsSocket = append(userSessionsSocket, structs.UserSessionSocket{ userSessionsSocket = append(userSessionsSocket, structs.UserSessionSocket{
IdForDeletion: userSession.IdForDeletion,
UserAgent: userSession.UserAgent, UserAgent: userSession.UserAgent,
ConnectionStatus: isUserSessionConnected(userSession.Id, socketClients), ConnectionStatus: isUserSessionConnected(userSession.Id, socketClients),
LastUsed: userSession.LastUsed, LastUsed: userSession.LastUsed,