Lumen:none 用户 table 的 JWT 身份验证

Lumen: JWT Authentication with none users table

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

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

一点背景:

我有一个userstable,还有一个pincodestable,userstable有两列mobile_numberstatus,我正在向用户 table 中的手机号码发送短信,短信有密码,然后我将该密码与 user_id 一起保存在 pincodes table.

所以身份验证将应用于 pincodes,我有 user_idcode 来验证用户是否真实。我正在使用带有 JWT 身份验证 library 的 lumen 微框架。所以我将 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(如下所示),参数为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
}

您使用的代码库使用了 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();
}