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

39 lines
1.0 KiB
Go
Raw Normal View History

2022-01-21 06:48:07 +00:00
package mongodb
import (
2022-07-10 16:19:33 +00:00
"context"
2022-01-21 06:48:07 +00:00
"time"
"github.com/authorizerdev/authorizer/server/db/models"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
// 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
session.CreatedAt = time.Now().Unix()
session.UpdatedAt = time.Now().Unix()
sessionCollection := p.db.Collection(models.Collections.Session, options.Collection())
2022-07-10 16:19:33 +00:00
_, err := sessionCollection.InsertOne(ctx, session)
2022-01-21 06:48:07 +00:00
if err != nil {
return err
}
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
sessionCollection := p.db.Collection(models.Collections.Session, options.Collection())
2022-07-10 16:19:33 +00:00
_, err := sessionCollection.DeleteMany(ctx, bson.M{"user_id": userId}, options.Delete())
2022-01-21 06:48:07 +00:00
if err != nil {
return err
}
return nil
}