Skip to content

Commit

Permalink
Trying to fix fiber middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubhrajyoti-Dey-FrosTiK committed Feb 6, 2024
1 parent 1c6243f commit a25e557
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions handler/fiber-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,34 @@ package handler

import (
"errors"
"net/http"
"net/http/httptest"

"github.com/FrosTiK-SD/auth/constants"
"github.com/FrosTiK-SD/auth/controller"
"github.com/FrosTiK-SD/auth/interfaces"
"github.com/FrosTiK-SD/auth/util"
"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
)

// For Gin based middlewares
func (h *Handler) FiberVerifyStudent(ctx *fiber.Ctx) error {
token := ctx.Get("token", "")

// Create a new session
currentHandler := Handler{
MongikClient: h.MongikClient,
JwkSet: h.JwkSet,
Session: &Session{},
Config: Config{
Mode: MIDDLEWARE,
},
}

context, _ := gin.CreateTestContext(httptest.NewRecorder())
context.Request = &http.Request{
Header: http.Header{
"token": []string{token},
},
idToken := ctx.Get("token", "")
noCache := false
if ctx.Get("cache-control") == constants.NO_CACHE {
noCache = true
}

currentHandler.HandlerVerifyStudentIdToken(context)
student := currentHandler.Session.Student
email, _, err := controller.VerifyToken(h.MongikClient.CacheClient, idToken, h.JwkSet, noCache)

if student != nil {
ctx.Locals(constants.SESSION, student)
ctx.Next()
} else {
return currentHandler.Session.Error
if err != nil {
return errors.New(*err)
}
student, err := controller.GetUserByEmail(h.MongikClient, email, &constants.ROLE_STUDENT, noCache)
if err != nil {
return errors.New(*err)
}

ctx.Locals(constants.SESSION, student)
ctx.Next()

return nil
}
Expand Down

0 comments on commit a25e557

Please sign in to comment.