develop #5

Merged
zeni merged 3 commits from develop into main 2024-10-23 08:12:37 -04:00
Showing only changes of commit 3c1170bd87 - Show all commits

View file

@ -5,7 +5,6 @@
package utils package utils
import ( import (
"crypto/md5"
"errors" "errors"
"fmt" "fmt"
"sync" "sync"
@ -37,7 +36,7 @@ func (s *SessionUser) Init(c *core.Context) bool {
s.context = c s.context = c
payload := make(map[string]interface{}) payload := make(map[string]interface{})
// get cookie
usercookie, err := c.GetCookie() usercookie, err := c.GetCookie()
if err != nil { if err != nil {
@ -54,13 +53,16 @@ func (s *SessionUser) Init(c *core.Context) bool {
payload, err = c.GetJWT().DecodeToken(token) payload, err = c.GetJWT().DecodeToken(token)
if err != nil { if err != nil {
pass = false pass = false
} else { } else {
userID := uint(c.CastToInt(payload["userID"])) userID := uint(c.CastToInt(payload["userID"]))
userAgent := c.GetUserAgent() userAgent := c.GetUserAgent()
hashedCacheKey := CreateAuthTokenHashedCacheKey(userID, userAgent)
// get data from redis
hashedCacheKey := CreateAuthTokenHashedCacheKey(userID, userAgent)
cachedToken, err := c.GetCache().Get(hashedCacheKey) cachedToken, err := c.GetCache().Get(hashedCacheKey)
if err != nil { if err != nil {
@ -73,28 +75,19 @@ func (s *SessionUser) Init(c *core.Context) bool {
if res.Error != nil && !errors.Is(res.Error, gorm.ErrRecordNotFound) { if res.Error != nil && !errors.Is(res.Error, gorm.ErrRecordNotFound) {
pass = false pass = false
} }
// if have session start the struct // if have session start the struct
if pass { if pass {
userAgent := c.GetUserAgent() userAgent := c.GetUserAgent()
sessionKey := fmt.Sprintf("session%v_%v", userID, userAgent) sessionKey := fmt.Sprintf("sess_%v", userAgent)
s.hashedSessionKey = fmt.Sprintf("%v", fmt.Sprintf("%x", md5.Sum([]byte(sessionKey)))) s.hashedSessionKey = CreateAuthTokenHashedCacheKey(userID, sessionKey)
value, err := c.GetCache().Get(s.hashedSessionKey)
if err != nil {
s.values = make(map[string]interface{}) s.values = make(map[string]interface{})
s.authenticated = true s.authenticated = true
s.userID = userID s.userID = userID
value, _ := c.GetCache().Get(s.hashedSessionKey)
if len(value) > 0 { if len(value) > 0 {
err := json.Unmarshal([]byte(value), &s.values) _ = json.Unmarshal([]byte(value), &s.values)
if err != nil {
}
} else {
}
} }
return true return true
@ -142,9 +135,9 @@ func (s *SessionUser) Delete(key string) interface{} {
func (s *SessionUser) Flush() error { func (s *SessionUser) Flush() error {
s.mu.Lock() s.mu.Lock()
s.values = make(map[string]interface{}) s.context.GetCache().Delete(s.hashedSessionKey)
s.mu.Unlock() s.mu.Unlock()
return s.Save() return nil
} }
func (s *SessionUser) Save() error { func (s *SessionUser) Save() error {
@ -160,15 +153,14 @@ func (s *SessionUser) Save() error {
return err return err
} }
value = string(buf) value = string(buf)
} }
s.mu.RUnlock()
if len(value) > 0 { if len(value) > 0 {
s.context.GetCache().Set(s.hashedSessionKey, value) s.context.GetCache().Set(s.hashedSessionKey, value)
} else {
s.context.GetCache().Delete(s.hashedSessionKey)
} }
s.mu.RUnlock()
return nil return nil
} }