简体   繁体   English

无法在 Laravel 中使用 tymon jwt 生成令牌

[英]Unable to generate token using tymon jwt in laravel

I'm stuck with this for about 3 days.我坚持了大约 3 天。 Basically, i'm trying to generate a JWT token in laravel using Tymon.基本上,我正在尝试使用 Tymon 在 laravel 中生成 JWT 令牌。 This is my controller file.这是我的控制器文件。

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use JWTAuth;
use JWT;
use Tymon\JWTAuthExceptions\JWTException;
use Tymon\JWTAuth\Contracts\JWTSubject as JWTSubject;

class AuthenticateController extends Controller
{
 public function index()
{
 //   
}
 public function authenticate(Request $request)
{

    $user = User::where('email', $request->only('email'))->first(); 
    dd($user); //This does show some output      
    $token = JWTAuth::fromUser($user); //returns error message

    return ["error" => NULL, "token" => $token];

 }
}

I tested this api using Chrome postman, but it is reporting this error:我使用 Chrome postman 测试了这个 api,但它报告了这个错误:

ErrorException in JWT.php line 73: Argument 1 passed to Tymon\\JWTAuth\\JWT::fromUser() must be an instance of Tymon\\JWTAuth\\Contracts\\JWTSubject, instance of App\\User given, called in /Users/shankerm/mccadmin/laravel/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 217 and defined JWT.php 第 73 行中的 ErrorException:传递给 Tymon\\JWTAuth\\JWT::fromUser() 的参数 1 必须是 Tymon\\JWTAuth\\Contracts\\JWTSubject 的实例,给定的 App\\User 实例,在 /Users/shankerm/mccadmin 中调用/laravel/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php 在第 217 行并定义

Pls give me some advice.请给我一些建议。 Im new to Laravel and struggling with this for a long time.我是 Laravel 的新手并且为此苦苦挣扎了很长时间。 Thanks.谢谢。

You are using the newer version of the package.您正在使用较新版本的软件包。 This requires that the User Model implements this contract.这需要User Model实现这个契约。 Solve it by doing this in your model:通过在您的模型中执行此操作来解决它:

use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Model implements JWTSubject {

Firstly you need to implement the Tymon\\JWTAuth\\Contracts\\JWTSubject contract on your User model, which requires that you implement the two methods getJWTIdentifier() and getJWTCustomClaims().首先你需要在你的 User 模型上实现 Tymon\\JWTAuth\\Contracts\\JWTSubject 合约,这需要你实现两个方法 getJWTIdentifier() 和 getJWTCustomClaims()。

Below is the example how your code could look.下面是您的代码的外观示例。 please make any changes necessary to suit your own needs.请进行任何必要的更改以满足您自己的需要。

<?php

namespace App;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    // Rest omitted for brevity

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}

Configure Auth guard配置身份验证保护

Note: This will only work if you are using Laravel 5.2 and above.注意:这仅在您使用 Laravel 5.2 及更高版本时有效。

Inside the config/auth.php file you will need to make a few changes to configure Laravel to use the jwt guard to power your application authentication.在 config/auth.php 文件中,您需要进行一些更改以配置 Laravel 以使用 jwt 防护来支持您的应用程序身份验证。

Make the following changes to the file:对文件进行以下更改:

'defaults' => [
    'guard' => 'api',
    'passwords' => 'users',
],

...

'guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

Here we are telling the api guard to use the jwt driver, and we are setting the api guard as the default.这里我们告诉 api 守卫使用 jwt 驱动程序,我们将 api 守卫设置为默认值。

Two issues that could be happening according to previous experience根据以前的经验可能会发生的两个问题

  1. Setting the jwt as the default auth driver.将 jwt 设置为默认身份验证驱动程序。 You can do it like this你可以这样做

    'api' => [ 'driver' => 'jwt', 'provider' => 'users', 'hash' => false, ], 'api' => [ 'driver' => 'jwt', 'provider' => 'users', 'hash' => false, ],

    1. Not implementing the JWTSubject in your preferred Model ie for a user model you can do it like this不在您的首选模型中实现 JWTSubject,即对于用户模型,您可以这样做

    class User extends Authenticatable implements JWTSubject{ then implement the functions by the interface as below类 User extends Authenticatable 实现 JWTSubject{ 然后通过接口实现功能如下

    /** * @inheritDoc */ public function getJWTIdentifier() { return $this->getKey(); /** * @inheritDoc */ public function getJWTIdentifier() { return $this->getKey(); } }

     /** * @inheritDoc */ public function getJWTCustomClaims() { return []; }

it could also be you have not generated a jwt auth key也可能是您没有生成 jwt auth 密钥

php artisan jwt:secret

with this you should have a entry like this in your .env有了这个,你应该在你的 .env 中有一个这样的条目

JWT_SECRET={value generated}

除了JWT 文档中提供的步骤之外,请评论 config/auth.php 中的以下可用部分,该部分在安装 laravel 后默认提供。

'users' => ['driver' => 'database', 'table' => 'users',],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM