mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-27 20:25:41 +00:00
refactor: use gorm generics api for database actions
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -41,6 +43,7 @@ type AuthService struct {
|
|||||||
loginMutex sync.RWMutex
|
loginMutex sync.RWMutex
|
||||||
ldap *LdapService
|
ldap *LdapService
|
||||||
database *gorm.DB
|
database *gorm.DB
|
||||||
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapService, database *gorm.DB) *AuthService {
|
func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapService, database *gorm.DB) *AuthService {
|
||||||
@@ -54,6 +57,7 @@ func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapS
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (auth *AuthService) Init() error {
|
func (auth *AuthService) Init() error {
|
||||||
|
auth.ctx = context.Background()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,7 +217,7 @@ func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.Sessio
|
|||||||
OAuthName: data.OAuthName,
|
OAuthName: data.OAuthName,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = auth.database.Create(&session).Error
|
err = gorm.G[model.Session](auth.database).Create(auth.ctx, &session)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -231,10 +235,10 @@ func (auth *AuthService) DeleteSessionCookie(c *gin.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
res := auth.database.Unscoped().Where("uuid = ?", cookie).Delete(&model.Session{})
|
_, err = gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).Delete(auth.ctx)
|
||||||
|
|
||||||
if res.Error != nil {
|
if err != nil {
|
||||||
return res.Error
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.SetCookie(auth.config.SessionCookieName, "", -1, "/", fmt.Sprintf(".%s", auth.config.CookieDomain), auth.config.SecureCookie, true)
|
c.SetCookie(auth.config.SessionCookieName, "", -1, "/", fmt.Sprintf(".%s", auth.config.CookieDomain), auth.config.SecureCookie, true)
|
||||||
@@ -249,15 +253,13 @@ func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie,
|
|||||||
return config.SessionCookie{}, err
|
return config.SessionCookie{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var session model.Session
|
session, err := gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).First(auth.ctx)
|
||||||
|
|
||||||
res := auth.database.Unscoped().Where("uuid = ?", cookie).First(&session)
|
if err != nil {
|
||||||
|
return config.SessionCookie{}, err
|
||||||
if res.Error != nil {
|
|
||||||
return config.SessionCookie{}, res.Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.RowsAffected == 0 {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return config.SessionCookie{}, fmt.Errorf("session not found")
|
return config.SessionCookie{}, fmt.Errorf("session not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user