2022-01-21 06:48:07 +00:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2022-01-31 06:05:24 +00:00
|
|
|
"time"
|
2022-01-21 06:48:07 +00:00
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddSession to save session information in database
|
|
|
|
func (p *provider) AddSession(session models.Session) error {
|
|
|
|
if session.ID == "" {
|
|
|
|
session.ID = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
session.Key = session.ID
|
2022-01-31 06:05:24 +00:00
|
|
|
session.CreatedAt = time.Now().Unix()
|
|
|
|
session.UpdatedAt = time.Now().Unix()
|
2022-01-21 06:48:07 +00:00
|
|
|
res := p.db.Clauses(
|
|
|
|
clause.OnConflict{
|
|
|
|
DoNothing: true,
|
|
|
|
}).Create(&session)
|
|
|
|
if res.Error != nil {
|
|
|
|
log.Println(`error saving session`, res.Error)
|
|
|
|
return res.Error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSession to delete session information from database
|
|
|
|
func (p *provider) DeleteSession(userId string) error {
|
|
|
|
result := p.db.Where("user_id = ?", userId).Delete(&models.Session{})
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error deleting session:`, result.Error)
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|