首页 > 解决方案 > 尝试在 Laravel 中使用 JWTAuth 创建用户时出错

问题描述

我正在尝试制作一个简单的身份验证 API,以使用 PHP 和 Laravel 的 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 的新手,我错过了什么吗?

标签: phplaravellaravel-5jwtjwt-auth

解决方案


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


推荐阅读