【问题标题】:Laravel JWT: Unable to implement JWTSubjectLaravel JWT:无法实现 JWTSubject
【发布时间】:2020-12-15 22:52:26
【问题描述】:

我是 laravel 的新手,我打算对我的 API 调用进行身份验证。就这样吧……

我已经按照 guide 在 laravel 上设置 jwt-auth 的步骤进行了操作

安装后,我在composer.json中更改了jwt-auth版本,运行composer update

"require": {
        "php": "^7.2.5",
        "fideloper/proxy": "^4.2",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/framework": "^7.24",
        "laravel/tinker": "^2.0",
        "tymon/jwt-auth": "^1.0.0-rc.5.1" //Originally "^1.0"
    },

我不知道这是否是原因,但是,在我的 User.php 模型中,我无法实现 JWTSubject

这是 User.php 模型:

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements App\JWTSubject //It says 'undefined type App\App\JWTSubject
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

更新:发现新问题:
我决定继续阅读指南,但是,在创建 AuthController.php 之后,我注意到 attemptlogoutrefreshfactory 方法有错误说 undefined method

【问题讨论】:

    标签: laravel jwt jwt-auth


    【解决方案1】:

    你不需要再使用implements App\JWTSubject,你已经用过了

    所以,implements 它只有 JWTSubject

    class User extends Authenticatable implements JWTSubject
    

    请查看document

    【讨论】:

    猜你喜欢
    • 2021-02-14
    • 2018-01-09
    • 2020-06-26
    • 2017-07-25
    • 2017-04-03
    • 2019-07-11
    • 2018-11-27
    • 2015-03-22
    • 2017-09-18
    相关资源
    最近更新 更多