JWT:无法从请求中解析令牌

时间:2018-08-07 07:52:15

标签: php laravel authentication jwt

我在控制器中将jwt用于CRUD,我有产品并且想要将$ user_id保存在数据库中:

  public function store(Request $request)
  {
    $this->user = JWTAuth::parseToken()->authenticate();
    $this->validate($request, [
      'name' => 'required',
      'price' => 'required|integer',
      'quantity' => 'required|integer'
    ]);

    $product = new Product();
    $product->name = $request->name;
    $product->price = $request->price;
    $product->quantity = $request->quantity;
    $product->user_id = $this->user;
    $product->save;

    if ($product)
      return response()->json([
        'success' => true,
        'product' => $product
      ]);
    else
      return response()->json([
        'success' => false,
        'message' => 'Sorry, product could not be added'
      ], 500);
  }

它工作正常,但是我用过:

  

$ this-> user = JWTAuth :: parseToken()-> authenticate();

在controller的某些方法中,所以我想避免重复该操作并将其写入控制器的构造中,但是在运行时:

  

php artisan route:list

它显示此错误:

  

无法从请求中解析令牌

1 个答案:

答案 0 :(得分:2)

您使用的JWTAuth版本无法与Laravel很好地集成,因此应该避免使用它。

此外,得到此信息的原因是调用构造函数时请求不正确。相反,打开您的基本控制器(通常为Public Function WorkingDays(StartDate As Date, EndDate As Date) As Integer '.................................................................... ' Name: WorkingDays ' Inputs: StartDate As Date ' EndDate As Date ' Returns: Integer ' Author: Arvin Meyer ' Date: February 19, 1997 ' Comment: Accepts two dates and returns the number of weekdays between them ' Note that this function does not account for holidays. '.................................................................... On Error GoTo Err_WorkingDays Dim intCount As Integer intCount = 0 Do While StartDate <= EndDate 'Make the above < and not <= to not count the EndDate Select Case Weekday(StartDate) Case Is = 1, 7 intCount = intCount Case Is = 2, 3, 4, 5, 6 intCount = intCount + 1 End Select StartDate = StartDate + 1 Loop WorkingDays = intCount Exit_WorkingDays: Exit Function Err_WorkingDays: Select Case Err Case Else MsgBox Err.Description Resume Exit_WorkingDays End Select End Function )并添加以下方法。

app/Http/Controllers/Controller

这样,只要您想要用户,就可以执行protected function user() { return JWTAuth::parseToken()->authenticate(); }