猿问

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

我使用 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 中的代码:


<?php


namespace 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:未定义类型响应:未定义类型


谁能帮我成功发帖?


侃侃无极
浏览 191回答 1
1回答

汪汪一只猫

你必须更新你的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 [];    }}
随时随地看视频慕课网APP
我要回答