douchao1957 2017-10-16 11:21
浏览 88

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

My user model:

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',
];
}

In the controller I have:

 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'));
}

I have used Hash::make($data->password) when creating the user.When I test it in Postman I always get "invalid credential"

  • 写回答

1条回答 默认 最新

  • dongping9475 2018-09-04 04:21
    关注

    if you are using version 0.5.* and kept the default settings as per the documentation, the JWTAuth::attempt($credentials) part will look for the input values 'email' and 'passowrd' $request->only('email', 'password'); In your case you are using $request->only('username', 'password');

    评论

报告相同问题?

悬赏问题

  • ¥15 php 将rtmp协议转hls协议,无法播放
  • ¥15 miniconda安装不了
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word