FatalThrowableError:Argument 1 passed ..::fromUser

2019-08-23 17:41发布

问题:

I use postman to add user or to login , user has added with sucess but I get this error

"Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given, called in C:\Users\Web WorkStation\Desktop\laravelapp\jwtlaravel\vendor\tymon\jwt-auth\src\JWTAuth.php on line 54"

and I found in this ligne this founction

 public function attempt(array $credentials)
    {
        if (! $this->auth->byCredentials($credentials)) {
            return false;
        }

        return $this->fromUser($this->user());
    }

and this my user model :

    <?php

    namespace App;


    use Tymon\JWTAuth\Contracts\JWTSubject;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    class User extends Authenticatable
    {
        use Notifiable;

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

        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];
        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 [];
        }
    }
and this my register controller

<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use JWTFactory;
use JWTAuth;
use Validator;
use Response;

class APIRegisterController extends Controller
{
    //
    public function register( Request $request){
        $validator = Validator::make($request -> all(),[
         'email' => 'required|string|email|max:255|unique:users',
         'username' =>'required',
         'tel' => 'required',
         'name' => 'required',
         'lastname' => 'required',
         'adress' => 'required',
         'password'=> 'required'
        ]);

        if ($validator -> fails()) {
            # code...
            return response()->json($validator->errors());

        }

        User::create([
            'name' => $request->get('name'),
            'email' => $request->get('email'),
            'tel' => $request->get('tel'),
            'username' => $request->get('username'),
            'lastname' => $request->get('lastname'),
            'adress' => $request->get('adress'),
            'password'=> bcrypt($request->get('password'))
        ]);
        $user = User::first();
        $token = JWTAuth::fromUser($user);

        return Response::json( compact('token'));


    }
}

回答1:

Implement the JWTSubject in your User model:

class User extends Authenticatable implements JWTSubject