diff --git a/action_create.go b/action_create.go index c98bf45..8f53290 100644 --- a/action_create.go +++ b/action_create.go @@ -63,6 +63,7 @@ func (payload CreateOptions) newDevice() Device { // Get userAgent data osName, osVersion, isMobile := getUserAgentData(payload.UserAgent) + userID, _ := mongodb.NewIDFromString(payload.UserID) return Device{ ID: mongodb.NewObjectID(), DeviceID: payload.DeviceID, @@ -76,7 +77,7 @@ func (payload CreateOptions) newDevice() Device { FCMToken: payload.FCMToken, Model: payload.Model, Manufacturer: payload.Manufacturer, - UserID: payload.UserID, + UserID: userID, IsMobile: isMobile, AppVersion: payload.AppVersion, } diff --git a/action_get_all.go b/action_get_all.go index 38a3819..70d73ed 100644 --- a/action_get_all.go +++ b/action_get_all.go @@ -4,6 +4,7 @@ import ( "context" "github.com/Selly-Modules/logger" + "github.com/Selly-Modules/mongodb" "go.mongodb.org/mongo-driver/bson" ) @@ -13,8 +14,9 @@ func (s Service) FindAllDevicesByUserID(userID string) []Device { ctx = context.Background() col = s.getDeviceCollection() result = make([]Device, 0) + id, _ = mongodb.NewIDFromString(userID) cond = bson.M{ - "userId": userID, + "userId": id, } ) diff --git a/action_get_user_id.go b/action_get_user_id.go index aa0f829..42930bc 100644 --- a/action_get_user_id.go +++ b/action_get_user_id.go @@ -27,6 +27,6 @@ func (s Service) GetUserIDByAuthToken(authToken string) (userID string) { return } - userID = device.UserID + userID = device.UserID.Hex() return } diff --git a/model.go b/model.go index 5a73a5d..68c51f0 100644 --- a/model.go +++ b/model.go @@ -17,7 +17,7 @@ type Device struct { Language string `bson:"language" json:"language"` // vi, en IsMobile bool `bson:"isMobile" json:"isMobile"` LastActivatedAt time.Time `bson:"lastActivatedAt" json:"lastActivatedAt"` - UserID string `bson:"userId" json:"userId"` + UserID primitive.ObjectID `bson:"userId" json:"userId"` AuthToken string `bson:"authToken" json:"authToken"` FCMToken string `bson:"fcmToken" json:"fcmToken"` Model string `bson:"model" json:"model"` diff --git a/validate.go b/validate.go index aef6e2b..3df02f5 100644 --- a/validate.go +++ b/validate.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/Selly-Modules/logger" + "github.com/Selly-Modules/mongodb" ) func (co CreateOptions) validate() error { @@ -38,6 +39,9 @@ func (co CreateOptions) validate() error { }) return errors.New("no userID data") } + if _, isValid := mongodb.NewIDFromString(co.UserID); !isValid { + return errors.New("invalid userID data") + } // AuthToken if co.AuthToken == "" {