Skip to content

Commit

Permalink
manually verify token in update student
Browse files Browse the repository at this point in the history
  • Loading branch information
shalearkane committed Apr 12, 2024
1 parent aba0b07 commit f1f8793
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion handler/student.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func GetStudentRoleObjectID() primitive.ObjectID {
}

func (h *Handler) HandlerUpdateStudentDetails(ctx *gin.Context) {
idToken := ctx.GetHeader("token")
studentCollection := h.MongikClient.MongoClient.Database(constants.DB).Collection(constants.COLLECTION_STUDENT)

var updatedStudent studentModel.Student
Expand All @@ -32,7 +33,18 @@ func (h *Handler) HandlerUpdateStudentDetails(ctx *gin.Context) {
return
}

filter := bson.M{"_id": h.Session.Student.Id, "email": h.Session.Student.InstituteEmail}
if email, _, errVerify := controller.VerifyToken(h.MongikClient.CacheClient, idToken, h.JwkSet, true); errVerify != nil {
ctx.AbortWithStatusJSON(401, gin.H{"error": errVerify})
return
} else {
if !util.CheckValidInstituteEmail(*email) {
ctx.AbortWithStatusJSON(401, gin.H{"error": "not a valid institute email"})
return
}
updatedStudent.InstituteEmail = *email
}

filter := bson.M{"_id": updatedStudent.RollNo, "email": updatedStudent.InstituteEmail}

var currentStudent studentModel.Student
if errFind := studentCollection.FindOne(ctx, filter).Decode(&currentStudent); errFind != nil {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func main() {
}

r.GET("/api/token/student/verify", handler.HandlerVerifyStudentIdToken)
r.PUT("/api/student/update", handler.GinVerifyStudent, handler.HandlerUpdateStudentDetails)
r.PUT("/api/student/update", handler.HandlerUpdateStudentDetails)
r.POST("/api/student/register", handler.HandlerRegisterStudentDetails)
r.GET("/api/token/invalidate_cache", handler.InvalidateCache)

Expand Down

0 comments on commit f1f8793

Please sign in to comment.