fix: refactor schema for open id claim standards

This commit is contained in:
Lakhan Samani
2021-12-22 10:51:12 +05:30
parent 8f7582e1ec
commit 508c714932
51 changed files with 1650 additions and 960 deletions

View File

@@ -61,11 +61,11 @@ func initArangodb() (arangoDriver.Database, error) {
}
}
userCollection, _ := arangodb.Collection(nil, Collections.User)
userCollection.EnsureHashIndex(ctx, []string{"id"}, &arangoDriver.EnsureHashIndexOptions{
userCollection.EnsureHashIndex(ctx, []string{"email"}, &arangoDriver.EnsureHashIndexOptions{
Unique: true,
Sparse: true,
})
userCollection.EnsureHashIndex(ctx, []string{"email"}, &arangoDriver.EnsureHashIndexOptions{
userCollection.EnsureHashIndex(ctx, []string{"phone_number"}, &arangoDriver.EnsureHashIndexOptions{
Unique: true,
Sparse: true,
})
@@ -79,11 +79,8 @@ func initArangodb() (arangoDriver.Database, error) {
log.Println("error creating collection("+Collections.VerificationRequest+"):", err)
}
}
verificationRequestCollection, _ := arangodb.Collection(nil, Collections.VerificationRequest)
verificationRequestCollection.EnsureHashIndex(ctx, []string{"id"}, &arangoDriver.EnsureHashIndexOptions{
Unique: true,
Sparse: true,
})
verificationRequestCollection.EnsureHashIndex(ctx, []string{"email", "identifier"}, &arangoDriver.EnsureHashIndexOptions{
Unique: true,
Sparse: true,
@@ -102,11 +99,5 @@ func initArangodb() (arangoDriver.Database, error) {
}
}
sessionCollection, _ := arangodb.Collection(nil, Collections.Session)
sessionCollection.EnsureHashIndex(ctx, []string{"id"}, &arangoDriver.EnsureHashIndexOptions{
Unique: true,
Sparse: true,
})
return arangodb, err
}

View File

@@ -46,6 +46,12 @@ func initMongodb() (*mongo.Database, error) {
Options: options.Index().SetUnique(true).SetSparse(true),
},
}, options.CreateIndexes())
userCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
mongo.IndexModel{
Keys: bson.M{"phone_number": 1},
Options: options.Index().SetUnique(true).SetSparse(true),
},
}, options.CreateIndexes())
mongodb.CreateCollection(ctx, Collections.VerificationRequest, options.CreateCollection())
verificationRequestCollection := mongodb.Collection(Collections.VerificationRequest, options.Collection())

View File

@@ -10,9 +10,9 @@ import (
)
type Session struct {
Key string `json:"_key,omitempty" bson:"_key,omitempty"` // for arangodb
ObjectID string `json:"_id,omitempty" bson:"_id"` // for arangodb & mongodb
ID string `gorm:"primaryKey;type:char(36)" json:"id" bson:"id"`
Key string `json:"_key,omitempty" bson:"_key,omitempty"` // for arangodb
// ObjectID string `json:"_id,omitempty" bson:"_id"` // for arangodb & mongodb
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
UserID string `gorm:"type:char(36)" json:"user_id" bson:"user_id"`
User User `json:"-" bson:"-"`
UserAgent string `json:"user_agent" bson:"user_agent"`
@@ -29,7 +29,7 @@ func (mgr *manager) AddSession(session Session) error {
if IsORMSupported {
session.Key = session.ID
session.ObjectID = session.ID
// session.ObjectID = session.ID
res := mgr.sqlDB.Clauses(
clause.OnConflict{
DoNothing: true,
@@ -53,7 +53,7 @@ func (mgr *manager) AddSession(session Session) error {
if IsMongoDB {
session.Key = session.ID
session.ObjectID = session.ID
// session.ObjectID = session.ID
session.CreatedAt = time.Now().Unix()
session.UpdatedAt = time.Now().Unix()
sessionCollection := mgr.mongodb.Collection(Collections.Session, options.Collection())

View File

@@ -14,19 +14,25 @@ import (
)
type User struct {
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
ObjectID string `json:"_id,omitempty" bson:"_id"` // for arangodb & mongodb
ID string `gorm:"primaryKey;type:char(36)" json:"id" bson:"id"`
FirstName string `json:"first_name" bson:"first_name"`
LastName string `json:"last_name" bson:"last_name"`
Email string `gorm:"unique" json:"email" bson:"email"`
Password string `gorm:"type:text" json:"password" bson:"password"`
SignupMethod string `json:"signup_method" bson:"signup_method"`
EmailVerifiedAt int64 `json:"email_verified_at" bson:"email_verified_at"`
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at" bson:"created_at"`
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at" bson:"updated_at"`
Image string `gorm:"type:text" json:"image" bson:"image"`
Roles string `json:"roles" bson:"roles"`
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
Email string `gorm:"unique" json:"email" bson:"email"`
EmailVerifiedAt int64 `json:"email_verified_at" bson:"email_verified_at"`
Password string `gorm:"type:text" json:"password" bson:"password"`
SignupMethods string `json:"signup_methods" bson:"signup_methods"`
GivenName string `json:"given_name" bson:"given_name"`
FamilyName string `json:"family_name" bson:"family_name"`
MiddleName string `json:"middle_name" bson:"middle_name"`
Nickname string `json:"nickname" bson:"nickname"`
Gender string `json:"gender" bson:"gender"`
Birthdate string `json:"birthdate" bson:"birthdate"`
PhoneNumber string `gorm:"unique" json:"phone_number" bson:"phone_number"`
PhoneNumberVerifiedAt int64 `json:"phone_number_verified_at" bson:"phone_number_verified_at"`
Picture string `gorm:"type:text" json:"picture" bson:"picture"`
Roles string `json:"roles" bson:"roles"`
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at" bson:"updated_at"`
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at" bson:"created_at"`
}
// AddUser function to add user even with email conflict
@@ -38,7 +44,7 @@ func (mgr *manager) AddUser(user User) (User, error) {
if IsORMSupported {
// copy id as value for fields required for mongodb & arangodb
user.Key = user.ID
user.ObjectID = user.ID
// user.ObjectID = user.ID
result := mgr.sqlDB.Clauses(
clause.OnConflict{
UpdateAll: true,
@@ -61,14 +67,14 @@ func (mgr *manager) AddUser(user User) (User, error) {
return user, err
}
user.Key = meta.Key
user.ObjectID = meta.ID.String()
// user.ObjectID = meta.ID.String()
}
if IsMongoDB {
user.CreatedAt = time.Now().Unix()
user.UpdatedAt = time.Now().Unix()
user.Key = user.ID
user.ObjectID = user.ID
// user.ObjectID = user.ID
userCollection := mgr.mongodb.Collection(Collections.User, options.Collection())
_, err := userCollection.InsertOne(nil, user)
if err != nil {
@@ -102,7 +108,7 @@ func (mgr *manager) UpdateUser(user User) (User, error) {
}
user.Key = meta.Key
user.ObjectID = meta.ID.String()
// user.ObjectID = meta.ID.String()
}
if IsMongoDB {

View File

@@ -13,9 +13,9 @@ import (
)
type VerificationRequest struct {
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
ObjectID string `json:"_id,omitempty" bson:"_id"` // for arangodb & mongodb
ID string `gorm:"primaryKey;type:char(36)" json:"id" bson:"id"`
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
// ObjectID string `json:"_id,omitempty" bson:"_id"` // for arangodb & mongodb
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
Token string `gorm:"type:text" json:"token" bson:"token"`
Identifier string `gorm:"uniqueIndex:idx_email_identifier" json:"identifier" bson:"identifier"`
ExpiresAt int64 `json:"expires_at" bson:"expires_at"`
@@ -32,7 +32,7 @@ func (mgr *manager) AddVerification(verification VerificationRequest) (Verificat
if IsORMSupported {
// copy id as value for fields required for mongodb & arangodb
verification.Key = verification.ID
verification.ObjectID = verification.ID
// verification.ObjectID = verification.ID
result := mgr.sqlDB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "email"}, {Name: "identifier"}},
DoUpdates: clause.AssignmentColumns([]string{"token", "expires_at"}),
@@ -54,14 +54,14 @@ func (mgr *manager) AddVerification(verification VerificationRequest) (Verificat
return verification, err
}
verification.Key = meta.Key
verification.ObjectID = meta.ID.String()
// verification.ObjectID = meta.ID.String()
}
if IsMongoDB {
verification.CreatedAt = time.Now().Unix()
verification.UpdatedAt = time.Now().Unix()
verification.Key = verification.ID
verification.ObjectID = verification.ID
// verification.ObjectID = verification.ID
verificationRequestCollection := mgr.mongodb.Collection(Collections.VerificationRequest, options.Collection())
_, err := verificationRequestCollection.InsertOne(nil, verification)
if err != nil {