samedi 2 septembre 2017

JWT Authentication user_not_found Tymon

I have set up Tymon Package for JWT Authentication. In case of new user sign up or login I get the token successfully. But when I pass the token to the Laravel JWT I get an error as user not found.

controller code

public function authenticate()
    {
        $credentials = request()->only('user_name','password');
        try{
            $token = JWTAuth::attempt($credentials);
            if(!$token){
                return response()->json(['error'=>'invalid_credentials'],401);
            }
        }
        catch(JWTException $e){
            return response()->json(['error'=>'something went wrong'],500);
        }
        return response()->json(['token'=>$token],200);
    }

    public function register()
    {
        $user_name = request()->user_name;
        $c_name = request()->company_name;
        $accessibility_level = request()->accessability_level;
        $password = request()->password;
        $contact_number = request()->contact_number;
        $address = request()->address;

        $user = User::create([
            'user_name'=>$user_name,
            'c_name'=>$c_name,
            'accessibility_level'=>$accessibility_level,
            'password'=>bcrypt($password),
            'contact_number'=>$contact_number,
            'address'=>$address
        ]);

        $token = JWTAuth::fromUser($user);

        return response()->json(['token'=>$token],200);
    }

no problem with the above code works fine.

But when I try to access some data with JWT validation I get an error as USER_NOT_FOUND. I have passed the Token which I have got as an header through Postman.

Route Code

Route::get('/some_route','some_controller@index')->middleware('jwt.auth');

And the jwt.php is also set with the correct identifier which I have used in the model(Primary key)

'identifier' => 'user_name',



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire