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

Viewed 16330

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 
        ]);
    }
3 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;
}

if anyone still getting this error that's because $verifiedIdToken->getClaim('sub') has been deprecated in 3.4 and removed in 4.0. use $verifiedIdToken->claims()->get('sub') this insted.

Related