使用JWT

时间:2017-10-16 11:21:12

标签: php laravel jwt postman

我的用户模型:

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->密码)。当我在Postman中测试时,我总是得到“无效凭证”

1 个答案:

答案 0 :(得分:1)

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