【问题标题】:Getting "invalid credentials" in Laravel using JWT使用 JWT 在 Laravel 中获取“无效凭据”
【发布时间】:2018-03-27 21:42:46
【问题描述】:

我的用户模型:

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】:

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

【讨论】:

    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 2018-12-16
    • 2020-04-10
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    相关资源
    最近更新 更多