我正在尝试在令牌身份验证中间件中提取 user_id 并将其传递给gqlgen的 graphql 解析器函数(以填充 GraphQL 模式的 created_by 和 updated_by 列)。身份验证部分可以正常工作。
Gin 中间件:
var UID = "dummy"
func TokenAuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
err := auth.TokenValid(c.Request)
if err != nil {
c.JSON(http.StatusUnauthorized, "You need to be authorized to access this route")
c.Abort()
return
}
//
UID, _ = auth.ExtractTokenID(c.Request)
//c.Set("user_id", UID)
c.Next()
}
}
func GetUID() string {
return UID
}
graphql 解析器:
var ConstID = middleware.GetUID()
func (r *mutationResolver) CreateFarmer(ctx context.Context, input model.NewFarmer) (*model.Farmer, error) {
//Fetch Connection and close db
db := model.FetchConnection()
defer db.Close()
//var ConstID, _ = uuid.NewRandom()
log.Println(ctx)
farmer := model.Farmer{Name: input.Name, Surname: input.Surname, Dob: input.Dob, Fin: input.Fin, PlotLocLat: input.PlotLocLat, PlotLocLong: input.PlotLocLong, CreatedAt: time.Now(), UpdatedAt: time.Now(), CreatedBy: ConstID, UpdatedBy: ConstID}
db.Create(&farmer)
return &farmer, nil
}
在这里,我尝试使用全局变量 UID 来执行此操作,但 UID 的值没有在中间件中更新,因此,我在CreatedBy
和UpdatedBy
列中获得了“虚拟”值。我知道不鼓励使用全局变量,我对其他想法持开放态度。谢谢