update/provide-student-id-in-auth

This commit is contained in:
SowinskiBraeden committed 2022-02-06 16:30:29 -08:00
1 parent 63623f693f
commit dd94f362a6
2 files changed
+10 -8

No files matched your search

+4 -4
View File
@@ -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 {
+6 -4
View File
@@ -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,