简体   繁体   English

Laravel:目标 [Lcobucci\JWT\Parser] 不可实例化

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

Hey I am having an issue on my prod website trying to log in with Laravel passport.嘿,我在我的产品网站上尝试使用 Laravel 护照登录时遇到问题。 It says my Lcobucci JWT Parser is not instantiable.它说我的 Lcobucci JWT 解析器不可实例化。 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:登录 Controller 方法:

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

I encountered the same issue as well, in your project composer.json add "lcobucci/jwt": "3.3.3" and execute composer update.我也遇到了同样的问题,在你的项目 composer.json 添加 "lcobucci/jwt": "3.3.3" 并执行 composer update。 I found this solution on: https://github.com/laravel/passport/issues/1381 .我在以下位置找到了这个解决方案: https://github.com/laravel/passport/issues/1381

Laravel: "8" and Lcobucci\JWT: "^3.4" Laravel:“8”和 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.如果有人仍然收到此错误,那是因为$verifiedIdToken->getClaim('sub')已在 3.4 中弃用并在 4.0 中删除。 use $verifiedIdToken->claims()->get('sub') this insted.使用$verifiedIdToken->claims()->get('sub')这个 insted。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM