update/provide-student-id-in-auth
This commit is contained in:
1 parent
63623f693f
commit
dd94f362a6
2 files changed
+10
-8
No files matched your search
@@ -48,14 +48,14 @@ func AuthAdmin(c *fiber.Ctx) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func AuthStudent(c *fiber.Ctx) bool {
|
func AuthStudent(c *fiber.Ctx) (verified bool, sid string) {
|
||||||
cookie := c.Cookies("jwt")
|
cookie := c.Cookies("jwt")
|
||||||
|
|
||||||
token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
|
token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||||
return []byte(SecretKey), nil
|
return []byte(SecretKey), nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
claims := token.Claims.(*jwt.StandardClaims)
|
claims := token.Claims.(*jwt.StandardClaims)
|
||||||
@@ -63,10 +63,10 @@ func AuthStudent(c *fiber.Ctx) bool {
|
|||||||
var student models.Admin
|
var student models.Admin
|
||||||
findErr := adminCollection.FindOne(context.TODO(), bson.M{"sid": claims.Issuer}).Decode(&student)
|
findErr := adminCollection.FindOne(context.TODO(), bson.M{"sid": claims.Issuer}).Decode(&student)
|
||||||
if findErr != nil {
|
if findErr != nil {
|
||||||
return false
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true, claims.Issuer
|
||||||
}
|
}
|
||||||
|
|
||||||
func Enroll(c *fiber.Ctx) error {
|
func Enroll(c *fiber.Ctx) error {
|
||||||
|
|||||||
@@ -621,8 +621,10 @@ func UpdateStudentEmail(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
verifiedAdmin := AuthAdmin(c)
|
||||||
|
verifiedStudent, sid := AuthStudent(c)
|
||||||
// Ensure Authenticated admin sent request
|
// Ensure Authenticated admin sent request
|
||||||
if !AuthAdmin(c) && !AuthStudent(c) {
|
if !verifiedAdmin && !verifiedStudent {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
@@ -631,7 +633,7 @@ func UpdateStudentEmail(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check required fields are included
|
// Check required fields are included
|
||||||
if data["sid"] == "" || data["email"] == "" {
|
if data["email"] == "" {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
@@ -649,7 +651,7 @@ func UpdateStudentEmail(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
result, updateErr := studentCollection.UpdateOne(
|
result, updateErr := studentCollection.UpdateOne(
|
||||||
ctx,
|
ctx,
|
||||||
bson.M{"schooldata.sid": data["sid"]},
|
bson.M{"schooldata.sid": sid},
|
||||||
update,
|
update,
|
||||||
)
|
)
|
||||||
if updateErr != nil {
|
if updateErr != nil {
|
||||||
@@ -837,7 +839,7 @@ func UpdateTeacherEmail(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ensure Authenticated admin sent request
|
// Ensure Authenticated admin sent request
|
||||||
if !AuthAdmin(c) && !AuthStudent(c) {
|
if !AuthAdmin(c) {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
|
|||||||
Reference in new issue
Block a user