2022-04-21 07:06:22 +00:00
|
|
|
package cassandradb
|
2022-03-19 12:11:27 +00:00
|
|
|
|
|
|
|
import (
|
2022-04-21 12:41:15 +00:00
|
|
|
"fmt"
|
2022-03-19 12:11:27 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddSession to save session information in database
|
|
|
|
func (p *provider) AddSession(session models.Session) error {
|
|
|
|
if session.ID == "" {
|
|
|
|
session.ID = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
session.CreatedAt = time.Now().Unix()
|
|
|
|
session.UpdatedAt = time.Now().Unix()
|
2022-04-21 12:41:15 +00:00
|
|
|
|
|
|
|
insertSessionQuery := fmt.Sprintf("INSERT INTO %s (id, user_id, user_agent, ip, created_at, updated_at) VALUES ('%s', '%s', '%s', '%s', %d, %d)", KeySpace+"."+models.Collections.Session, session.ID, session.UserID, session.UserAgent, session.IP, session.CreatedAt, session.UpdatedAt)
|
|
|
|
err := p.db.Query(insertSessionQuery).Exec()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-19 12:11:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSession to delete session information from database
|
|
|
|
func (p *provider) DeleteSession(userId string) error {
|
2022-04-21 12:41:15 +00:00
|
|
|
deleteSessionQuery := fmt.Sprintf("DELETE FROM %s WHERE user_id = '%s'", KeySpace+"."+models.Collections.Session, userId)
|
|
|
|
err := p.db.Query(deleteSessionQuery).Exec()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-19 12:11:27 +00:00
|
|
|
return nil
|
|
|
|
}
|