From ff65d9eadc08623021f7ea36dff0fc4f53da7135 Mon Sep 17 00:00:00 2001 From: Braeden57 Date: Tue, 12 Oct 2021 23:07:20 -0700 Subject: [PATCH] fix cid checks\ --- controllers/authController.go | 3 ++- controllers/idManager.go | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/controllers/authController.go b/controllers/authController.go index d1124ea..51914e3 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -172,7 +172,8 @@ func RegisterTeacher(c *fiber.Ctx) error { var tid string for { tid = GenerateID() - if ValidateID(tid) == true { + isValid := ValidateID(tid) + if isValid == true { break } } diff --git a/controllers/idManager.go b/controllers/idManager.go index 8019df0..cae08fb 100644 --- a/controllers/idManager.go +++ b/controllers/idManager.go @@ -19,13 +19,14 @@ var table = [...]byte{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'} func ValidateID(id string) bool { // true: valid id, false: id already in use ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - err := idCollection.FindOne(ctx, bson.M{"cid": id}) + var foundID models.Id + err := idCollection.FindOne(ctx, bson.M{"cid": id}).Decode(&foundID) cancel() - if err != nil && err.Err() == mongo.ErrNoDocuments { + if err != nil { var newID models.Id newID.CID = id newID.ID = primitive.NewObjectID() - _, insertErr := idCollection.InsertOne(ctx, newID) + _, insertErr := idCollection.InsertOne(context.Background(), newID) if insertErr != nil { return false }