使用JWT在Laravel中获取“无效凭据”

问题描述 投票:0回答:1

我的用户模型:

namespace Modules\Settings\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Users extends Authenticatable
{
Protected $table="user";
// protected $primaryKey = 'id';
protected $fillable = ['id','username','password','user_status_type_id','client_id','created_userid'];

protected $hidden = [
    'password', 'remember_token',
];
}

在控制器中我有:

 public function login(Authentication $request){
   $credentials = $request->only('username', 'password');



    try {
        // verify the credentials and create a token for the user
        $token = JWTAuth::attempt($credentials);


        if (! $token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    // if no errors are encountered we can return a JWT
    return response()->json(compact('token'));
}

我在创建用户时使用了Hash :: make($ data-> password)。当我在Postman中测试时,我总是得到“无效凭据”

php laravel jwt postman
1个回答
1
投票

如果你使用版本0.5。*并按照文档保留默认设置,JWTAuth::attempt($credentials)部分将查找输入值'email'和'passowrd'$request->only('email', 'password');在你的情况下你使用$request->only('username', 'password');

© www.soinside.com 2019 - 2024. All rights reserved.