【问题标题】:Lumen: JWT Authentication with none users tableLumen:没有用户表的 JWT 身份验证
【发布时间】:2017-10-18 09:32:23
【问题描述】:

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

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

小背景:

我有一个users 表和一个pincodes 表,users 表有两列mobile_numberstatus,我正在向用户表中的手机号码发送短信,短信有一个秘密代码然后我将该代码与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');
    }
}

verify方法请求类型为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
}

【问题讨论】:

    标签: php laravel authentication jwt lumen


    【解决方案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();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-03
      • 2017-09-11
      • 2018-06-08
      • 2016-06-25
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      相关资源
      最近更新 更多