title: Go Gin Setting and Accessing context value from middleware
body: <p>I am trying to set my user context, in the middleware then trying to check if user have permission in other handler functions. But for some reason when I try to access the user from context it is coming back as nils. The middleware code seems to be working, when I pass a valid jwt token, it is showing the user is being set in context in the middleware function. But as soon as I hit getCurrentUser function it says it's nil.</p>
  
  <p>Here is the code:
  Middleware</p>
  
  <pre><code>// Middleware wraps the request with auth middleware
  func Middleware(path string, sc *cfg.Server, orm *orm.ORM) gin.HandlerFunc {
      logger.Info("[Auth.Middleware] Applied to path: ", path)
      return gin.HandlerFunc(func(c *gin.Context) {
          t, err := ParseToken(c, sc)
          if err != nil {
              authError(c, err)
          } else {
              if claims, ok := t.Claims.(jwt.MapClaims); ok {
                  if claims["exp"] != nil {
                      issuer := claims["iss"].(string)
                      userid := claims["jti"].(string)
                      email := claims["email"].(string)
                      if claims["aud"] != nil {
                          audiences := claims["aud"].(interface{})
                          logger.Warnf("\n\naudiences: %s\n\n", audiences)
                      }
                      if claims["alg"] != nil {
                          algo := claims["alg"].(string)
                          logger.Warnf("\n\nalgo: %s\n\n", algo)
                      }
                      if user, err := orm.FindUserByJWT(email, issuer, userid); err != nil {
                          authError(c, ErrForbidden)
                      } else {
                          if user != nil {
                              c.Request = addToContext(c, consts.ProjectContextKeys.UserCtxKey, user)
                              logger.Debug("User: ", user.ID)
                          }
                          c.Next()
                      }
                  } else {
                      authError(c, ErrMissingExpField)
                  }
              } else {
                  authError(c, err)
              }
          }
      })
  }
  </code></pre>
  
  <p>routes</p>
  
  <pre><code>// User routes
  func User(sc *cfg.Server, r *gin.Engine, orm *orm.ORM) error {
      // OAuth handlers
      mw := auth.Middleware(sc.VersionedEndpoint("/user/:id"), sc, orm)
      g := r.Group(sc.VersionedEndpoint("/user"))
      g.Use(mw)
      g.GET("/:id", mw, user.Get(orm))
      g.PUT("/:id", mw, user.Update(orm))
      g.POST("/", user.Create(orm))
  
      return nil
  }
  </code></pre>
  
  <p>handler</p>
  
  <pre><code>func Get(orm *orm.ORM) gin.HandlerFunc {
      return func(ctx *gin.Context) {
          cu := getCurrentUser(ctx)
          if ok, err := cu.HasPermission(consts.Permissions.Create, consts.EntityNames.Users); !ok || err != nil {
              ctx.String(http.StatusUnauthorized, "BAD")
          }
      }
  }
  
  </code></pre>
  
  <p>addToContext:</p>
  
  <pre><code>func addToContext(c *gin.Context, key consts.ContextKey, value interface{}) *http.Request {
      return c.Request.WithContext(context.WithValue(c.Request.Context(), key, value))
  }
  </code></pre>
  
  <p>getCurrentUser:</p>
  
  <pre><code>func getCurrentUser(ctx context.Context) *dbm.User {
      cu := ctx.Value(utils.ProjectContextKeys.UserCtxKey).(*dbm.User)
      logger.Debugf("currentUser: %s - %s", cu.Email, cu.ID)
      return cu
  }
  </code></pre>