为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel 新用户通过 API POST 注册失败

Laravel 新用户通过 API POST 注册失败

PHP
侃侃无极 2023-03-26 16:06:11
我使用 Postman 尝试通过 laravel 创建一个带有电子邮件和密码的新用户到数据库但是在成功创建用户后我没有收到令牌,而是收到了这些:    <!doctype html>   <html class="theme-light">   <!--TypeError: 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/ahmed/laravelapi/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 261 in file /Users/ahmed/laravelapi/vendor/tymon/jwt-auth/src/JWT.php on line 88#0 /Users/ahmed/laravelapi/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(261): Tymon\JWTAuth\JWT->fromUser(Object(App\User))#1 /Users/ahmed/laravelapi/app/Http/Controllers/APIRegisterController.php(33): Illuminate\Support\Facades\Facade::__callStatic('fromUser', Array)#2 [internal function]: App\Http\Controllers\APIRegisterController->register(Object(Illuminate\Http\Request))#3 /Users/ahmed/laravelapi/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)#4 /Users/ahmed/laravelapi/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('register', Array)#5 /Users/ahmed/laravelapi/vendor/laravel/framework/src/Illuminate/Routing/Route.php(239): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\APIRegisterController), 'register')这是 APIRegisterController.php 中的代码:<?phpnamespace App\Http\Controllers;use App\Http\Controllers\Controller;use App\User;use JWTFactory;use JWTAuth;use Validator;use Response;use Illuminate\Http\Request;class APIRegisterController extends Controller{    public function register(Request $request){        $validator = Validator::make($request -> all(),[            'email' => 'required|string|email|max:255|unique:users',            'name' => 'required',            'password' => 'required'    ]);虽然我在 laravel 项目中收到这些错误:验证器:未定义类型 JWTAuth:未定义类型响应:未定义类型谁能帮我成功发帖?
查看完整描述

1 回答

?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

你必须更新你的User模型来实现Tymon\JWTAuth\Contracts\JWTSubject接口:

use Tymon\JWTAuth\Contracts\JWTSubject;


class User implements JWTSubject

{

    /**

     * 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 [];

    }

}


查看完整回答
反对 回复 2023-03-26
  • 1 回答
  • 0 关注
  • 190 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信