首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel: Target [Lcobucci\JWT\Parser]不可实例化

Laravel: Target [Lcobucci\JWT\Parser]不可实例化
EN

Stack Overflow用户
提问于 2020-11-26 00:44:46
回答 3查看 17.1K关注 0票数 15

嘿,我在我的prod网站上有个问题,想用Laravel护照登录。它说我的Lcobucci JWT解析器是不可实例化的。它在本地工作,但在我的遥控器上不起作用。

我怎么解决这个问题?

错误:

代码语言:javascript
运行
复制
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: [,…]

登录控制器方法:

代码语言:javascript
运行
复制
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 
        ]);
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-11-26 07:39:48

我也遇到了同样的问题,在您的项目composer.json中添加"lcobucci/jwt":"3.3.3“并执行composer更新。我在https://github.com/laravel/passport/issues/1381上找到了这个解决方案。

票数 26
EN

Stack Overflow用户

发布于 2021-04-16 12:16:57

Laravel:"8“和Lcobucci\JWT:"^3.4"

解决方案:

代码语言:javascript
运行
复制
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;
}
票数 12
EN

Stack Overflow用户

发布于 2022-07-15 05:47:02

如果有人仍然收到此错误,那是因为$verifiedIdToken->getClaim('sub')在3.4中被弃用,在4.0中被删除。使用$verifiedIdToken->claims()->get('sub')这个insted。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65014655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档