Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Target [Lcobucci\JWT\Parser] is not instantiable

Tags:

php

laravel

Hey I am having an issue on my prod website trying to log in with Laravel passport. It says my Lcobucci JWT Parser is not instantiable. It works for me locally but not on my remote.

How can I resolve this?

Error:

exception: "Illuminate\Contracts\Container\BindingResolutionException"
file: "/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php"
line: 1038
message: "Target [Lcobucci\JWT\Parser] is not instantiable while building [Laravel\Passport\PersonalAccessTokenFactory]."
trace: [,…]

Login Controller Method:

public function login(Request $request) {

        $login = $request->validate([
            'email' => 'required:string',
            'password' => 'required:string'
        ]);

        if(filter_var($request->email, FILTER_VALIDATE_EMAIL)) {
            //user sent their email 
            Auth::attempt(['email' => $request->email, 'password' => $request->password]);
        } else {
            //they sent their username instead 
            Auth::attempt(['username' => $request->email, 'password' => $request->password]);
        }

        if(!Auth::check()) {
            return response([
                'status' => 'fail',
                'message' => 'Invalid credentials'
            ]);
        }

        $accessToken = Auth::user()
            ->createToken('authToken')
            ->accessToken;
        
        return response([
            'status' => 'success',
            'user' => new User_Resource(Auth::user()),
            'access_token' => $accessToken 
        ]);
    }
like image 448
Eddsters Avatar asked Nov 26 '20 00:11

Eddsters


2 Answers

Laravel : "8" and Lcobucci\JWT : "^3.4"

Solution:

use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Token\Parser;
.
.
...
public function GetTokenId(Request $request)
{
  // Get the Access_Token from the request
  $Token = $request->bearerToken();
  // Parse the Access_Token to get the claims from them the jti(Json Token Id)
  $TokenId = (new Parser(new JoseEncoder()))->parse($token)->claims()
   ->all()['jti'];
  return $tokenId;
}
like image 136
Ace wings Avatar answered Oct 23 '22 00:10

Ace wings


I encountered the same issue as well, in your project composer.json add "lcobucci/jwt": "3.3.3" and execute composer update. I found this solution on: https://github.com/laravel/passport/issues/1381.

like image 26
Gams Basallo Avatar answered Oct 23 '22 00:10

Gams Basallo