gpt4 book ai didi

php - 方法 Laravel\\Passport\\Bridge\\AccessToken::__toString() 不能抛出异常

转载 作者:行者123 更新时间:2023-12-04 03:35:02 26 4
gpt4 key购买 nike

我使用这些包和这些版本来为用户登录创建 token ,但是我在创建 token 时遇到了这个错误:

composer.json

"require": {
"php": "^7.3|^8.0",
"fideloper/proxy": "^4.4",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.12",
"laravel/passport": "^10.1.3",
"laravel/tinker": "^2.5",
"laravel/ui": "^3.2",
"laravelcollective/html": "^6.2",
"lcobucci/jwt": "3.4.5",
"spatie/laravel-permission": "^4.0"
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^2.9",
"facade/ignition": "^2.5",
"fakerphp/faker": "^1.9.1",
"laravel/sail": "^1.0.1",
"mockery/mockery": "^1.4.2",
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^9.3.3"
}

routes/api.php

Route::post('login', [ApiLoginController::class, 'login']);

ApiLoginController.php

public function login(Request $request)
{
$this->validateLogin($request);

$user = User::where($this->username, $request->get('username'))
->first();

if (Auth::attempt([$this->username => $request->get('username'), 'password' => $request->get('password')])) {

if ($user->status != 'active') {
$msg = 'Account is not active';

return $this->customError($msg);
}

$success['token'] = $user->createToken('Personal Access Client')->accessToken;
$success['name'] = $user->name;
$success['phone'] = $user->phone;
$success['email'] = $user->email;

$user->password = null;
$user->save();

return $this->success($success, "Login completed successfully");

}

//delete user password
if ($user){
$user->password = null;
$user->save();
}

$msg = 'The information entered does not match our information';

return $this->customError($msg);
}

postman 响应

{
"message": "Method Laravel\\Passport\\Bridge\\AccessToken::__toString() must not throw an exception, caught Lcobucci\\JWT\\Signer\\InvalidKeyProvided: It was not possible to parse your key, reason: error:0908F070:PEM routines:get_header_and_data:short header",
"exception": "Symfony\\Component\\ErrorHandler\\Error\\FatalError",
"file": "F:\\xampp\\htdocs\\Diapad-BackEnd\\vendor\\league\\oauth2-server\\src\\ResponseTypes\\BearerTokenResponse.php",
"line": 0,
"trace": []
}

此过程无需 token 行即可运行。甚至创建了一个 token ,但没有作为字符串返回。我多次降低甚至升级我的软件包版本,但都没有用。

最佳答案

您可能使用的是 7.4 之前的 PHP 版本。

this RFC 允许在 __toString() 方法中抛出异常已被 PHP 7.4 接受

laravel/passportrelies on lcobucci/jwt以及所需的 league/oauth2-server also does .

lcobucci/jwt 有一个 minimum of PHP 7.4 dependency written你的 composer installcomposer update 应该在某个时候捕获,除非你没有自己安装它们或使用 composer --ignore-platform-reqs 标志。

关于php - 方法 Laravel\\Passport\\Bridge\\AccessToken::__toString() 不能抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67086544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com