流明:没有用户的JWT身份验证表

时间:2017-05-18 14:02:26

标签: php laravel authentication jwt lumen

简而言之,我收到以下错误:

{
  "message": "Undefined index: password",
  "status_code": 500
}

liitle背景:

我有一个users表和一个pincodes表,users表有两列mobile_numberstatus,我发送短信到用户表中的移动号码,Sms有一个密码,然后我将该代码与user_id表中的pincodes一起保存。

因此,身份验证将应用于pincodes,我有user_idcode以验证用户是否可信。我正在使用流明微框架,使用JWT身份验证library。所以我将Pincode模型更改为User模型。

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;

class Pincode extends Model implements
AuthenticatableContract,
AuthorizableContract {
    use Authenticatable, Authorizable;

    protected $table = 'pincodes';
    public function user() {
        return $this->belongsTo('App\User');
    }
}

验证方法请求类型为post(如下所示),params为user_idcode。我提供的是正确的。

令牌应该生成的验证方法:

public function verify(Request $request) {
  $uid = $request->get('uid');
  $pinCode = $request->get('code');

  $findPinCode = \App\Pincode::where('uid', $uid)->where('code', $pinCode)->first();
  if (!$findPinCode) {
      return response()->json([
          'message' => 'No pin Code found',
          'code' => 404,
      ]);
  }

  $findPinCode->status = 'v';
  $findPinCode->dateVerify = Carbon::now();

  $findPinCode->save();

  try {

      $this->validatePostLoginRequest($request);

  } catch (HttpResponseException $e) {
      return $this->onBadRequest();
  }

  try {
      if (!$token = JWTAuth::attempt(
          $this->getCredentials($request)
      )) {
          return 'asdasd';
          return $this->onUnauthorized();
      }
  } catch (JWTException $e) {
      return $this->onJwtGenerationError();
  }

我收到以下错误:

{
  "message": "Undefined index: password",
  "status_code": 500
}

1 个答案:

答案 0 :(得分:1)

您使用的代码库使用tymondesigns/jwt-auth包。默认情况下,JWTAuth::attempt方法使用电子邮件和密码。

最简单的方法是通过密码手动验证用户并获取用户对象并使用JWTAuth::fromUser

为用户生成令牌
$user = User::find($uid);

try {
    if (!$token = JWTAuth::fromUser($user)) {
        return $this->onUnauthorized();
    }
} catch (JWTException $e) {
    return $this->onJwtGenerationError();
}
相关问题