authorizer/server/db/providers/sql/session.go

40 lines
873 B
Go
Raw Normal View History

2022-01-21 06:48:07 +00:00
package sql
import (
2022-07-10 16:19:33 +00:00
"context"
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
2022-07-10 16:19:33 +00:00
func (p *provider) AddSession(ctx context.Context, session models.Session) error {
2022-01-21 06:48:07 +00:00
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 {
return res.Error
}
return nil
}
// DeleteSession to delete session information from database
2022-07-10 16:19:33 +00:00
func (p *provider) DeleteSession(ctx context.Context, userId string) error {
2022-01-21 06:48:07 +00:00
result := p.db.Where("user_id = ?", userId).Delete(&models.Session{})
if result.Error != nil {
return result.Error
}
return nil
}