diff --git a/controllers/authController.go b/controllers/authController.go index 01e2f70..8fb4c7f 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -48,14 +48,14 @@ func AuthAdmin(c *fiber.Ctx) bool { return true } -func AuthStudent(c *fiber.Ctx) bool { +func AuthStudent(c *fiber.Ctx) (verified bool, sid string) { cookie := c.Cookies("jwt") token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) { return []byte(SecretKey), nil }) if err != nil { - return false + return false, "" } claims := token.Claims.(*jwt.StandardClaims) @@ -63,10 +63,10 @@ func AuthStudent(c *fiber.Ctx) bool { var student models.Admin findErr := adminCollection.FindOne(context.TODO(), bson.M{"sid": claims.Issuer}).Decode(&student) if findErr != nil { - return false + return false, "" } - return true + return true, claims.Issuer } func Enroll(c *fiber.Ctx) error { diff --git a/controllers/updateController.go b/controllers/updateController.go index e8f6d69..9d0e37c 100644 --- a/controllers/updateController.go +++ b/controllers/updateController.go @@ -621,8 +621,10 @@ func UpdateStudentEmail(c *fiber.Ctx) error { }) } + verifiedAdmin := AuthAdmin(c) + verifiedStudent, sid := AuthStudent(c) // Ensure Authenticated admin sent request - if !AuthAdmin(c) && !AuthStudent(c) { + if !verifiedAdmin && !verifiedStudent { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -631,7 +633,7 @@ func UpdateStudentEmail(c *fiber.Ctx) error { } // Check required fields are included - if data["sid"] == "" || data["email"] == "" { + if data["email"] == "" { cancel() return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, @@ -649,7 +651,7 @@ func UpdateStudentEmail(c *fiber.Ctx) error { result, updateErr := studentCollection.UpdateOne( ctx, - bson.M{"schooldata.sid": data["sid"]}, + bson.M{"schooldata.sid": sid}, update, ) if updateErr != nil { @@ -837,7 +839,7 @@ func UpdateTeacherEmail(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) && !AuthStudent(c) { + if !AuthAdmin(c) { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false,