I use these packages with these versions to create tokens for user login, but I encounter this error when creating tokens:
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 Response
{
"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": []
}
This process works without a token line. Even a token is created but not returned as a string. I lowered or even upgraded my package version several times, but it didn't work.
You're probably on a PHP version prior to 7.4.
Throwing exceptions in the __toString()
method was allowed by this RFC which was accepted for PHP 7.4
The laravel/passport
package relies on lcobucci/jwt
as well as the required league/oauth2-server
also does.
lcobucci/jwt
has a minimum of PHP 7.4 dependency written which your composer install
or composer update
should have caught at some point unless you haven't installed them yourself or used composer
with the --ignore-platform-reqs
flag.