extended method to get auth data

This commit is contained in:
nquidox 2025-09-08 23:57:14 +03:00
parent 4c15e804fc
commit 360d265672
2 changed files with 26 additions and 10 deletions

View file

@ -5,7 +5,7 @@ import "github.com/gin-gonic/gin"
type Utils interface { type Utils interface {
IsEmail(email string) bool IsEmail(email string) bool
GetUserUuidFromContext(c *gin.Context) (string, error) GetUserUuidFromContext(c *gin.Context) (string, error)
GetUserAndTokenUuidFromContext(c *gin.Context) (string, string, error) GetAllTokensFromContext(c *gin.Context) (string, string, string, error)
HashPassword(password string) (string, error) HashPassword(password string) (string, error)
ComparePasswords(hashedPassword string, plainPassword string) error ComparePasswords(hashedPassword string, plainPassword string) error
} }

View file

@ -28,42 +28,58 @@ func (u *Utils) GetUserUuidFromContext(c *gin.Context) (string, error) {
return userUuid.String(), nil return userUuid.String(), nil
} }
func (u *Utils) GetUserAndTokenUuidFromContext(c *gin.Context) (string, string, error) { func (u *Utils) GetAllTokensFromContext(c *gin.Context) (string, string, string, error) {
if c == nil { if c == nil {
return "", "", errors.New("context is nil") return "", "", "", errors.New("context is nil")
} }
//get user uuid //get user uuid
userRaw, exists := c.Get("userUuid") userRaw, exists := c.Get("userUuid")
if !exists { if !exists {
return "", "", errors.New("user uuid not found in context") return "", "", "", errors.New("user uuid not found in context")
} }
userUuidStr, ok := userRaw.(string) userUuidStr, ok := userRaw.(string)
if !ok { if !ok {
return "", "", errors.New("user uuid is not a string") return "", "", "", errors.New("user uuid is not a string")
} }
userUuid, err := uuid.Parse(userUuidStr) userUuid, err := uuid.Parse(userUuidStr)
if err != nil { if err != nil {
return "", "", errors.New("error parsing user uuid") return "", "", "", errors.New("error parsing user uuid")
} }
//get refresh token uuid //get refresh token uuid
refreshRaw, exists := c.Get("refreshUuid") refreshRaw, exists := c.Get("refreshUuid")
if !exists { if !exists {
return "", "", errors.New("user uuid not found in context") return "", "", "", errors.New("refresh uuid not found in context")
} }
refreshUuidStr, ok := refreshRaw.(string) refreshUuidStr, ok := refreshRaw.(string)
if !ok { if !ok {
return "", "", errors.New("user uuid is not a string") return "", "", "", errors.New("refresh uuid is not a string")
} }
refreshUuid, err := uuid.Parse(refreshUuidStr) refreshUuid, err := uuid.Parse(refreshUuidStr)
if err != nil { if err != nil {
return "", "", errors.New("error parsing user uuid") return "", "", "", errors.New("error parsing refresh uuid")
} }
return userUuid.String(), refreshUuid.String(), nil //get session token uuid
sessionRaw, exists := c.Get("sessionUuid")
if !exists {
return "", "", "", errors.New("session uuid not found in context")
}
sessionUuidStr, ok := sessionRaw.(string)
if !ok {
return "", "", "", errors.New("session uuid is not a string")
}
sessionUuid, err := uuid.Parse(sessionUuidStr)
if err != nil {
return "", "", "", errors.New("error parsing session uuid")
}
return userUuid.String(), refreshUuid.String(), sessionUuid.String(), nil
} }