【问题标题】:Error when trying to create an user with JWTAuth in Laravel [closed]尝试在 Laravel 中使用 JWT Auth 创建用户时出错 [关闭]
【发布时间】:2021-01-21 05:42:21
【问题描述】:

我正在尝试使用 PHP 和 Laravel 制作一个简单的身份验证 API 以使用 JWTAuth 获取令牌,但每次尝试创建用户时都会出现此错误:

[Tue Oct  6 07:21:21 2020] 127.0.0.1:56819 Accepted
[Tue Oct  6 07:21:22 2020] 127.0.0.1:56819 Closing
[Tue Oct  6 07:21:31 2020] 127.0.0.1:56820 Accepted
[Tue Oct  6 07:21:31 2020] PHP Fatal error:  Class App\Models\User contains 1 abstract method and must
 therefore be declared abstract or implement the remaining methods (Tymon\JWTAuth\Contracts\JWTSubject::getJWTCustomClaims) 
in E:\_Code\laravel\ApiDevBarber\app\Models\User.php on line 10
[Tue Oct  6 07:21:33 2020] 127.0.0.1:56820 Closing

Insomnia、Rest 测试测试和高级 REST 客户端都会引发此错误。我已经按照 Laravel 和 JWT 文档编写了 User.php,下面是整个代码。

<?php

namespace App\Models;

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

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $hidden = ['password'];

    public $timestamps = false;

    public function getJWTIdentifier() {
        return $this->getkey();
    }

    public function getJWTCustomClains() {
        return [];
    }
}

我是 PHP 和 Laravel 的新手,我错过了什么吗?

【问题讨论】:

  • 将您的方法 getJWTCustomClains 重命名为 getJWTCustomClaims
  • 我不相信……哈哈。我查看了代码并跳过了这个错字,这一定是因为我不是以英语为母语的人。谢谢@Dmitry。

标签: php laravel laravel-5 jwt jwt-auth


【解决方案1】:

您的方法 getJWTCustomClaims(而不是 getJWTCustomClains)中有错误

【讨论】:

  • 我们通常不会为简单的拼写错误写答案,评论和近距离投票(由拼写错误引起)就足够了。
猜你喜欢
  • 1970-01-01
  • 2020-12-30
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2020-12-04
  • 2021-03-31
  • 2017-02-13
相关资源
最近更新 更多