Laravel 5.5. jwt:generate issue
Closed this issue ยท 49 comments
Hi all,
I came upon one issue when I try to run php artisan jwt:generate
I get the Reflection Exception saying "[ReflectionException] Method Tymon\JWTAuth\Commands\JWTGenerateCommand::handle() does not exist"
Both Facades and service providers are entered properly and they are valid.
Also for people who have a struggle to make it work I can advise one solution. Go to JWTGenerateCommand.php file located in vendor/tymon/src/Commands and paste this part of code public function handle() { $this->fire(); }
I know this is not an elegant solution, but it works. I hope this might help until official fix arrive.
Have a nice day.
I guess this is already resolved by #1255.
Acho que nรฃo foi.
้ๅธธๆ่ฐข
I just added this package to my Laravel 5.5 project
Composer:
"tymon/jwt-auth": "0.5.*"
added the following to the app config:
Providers
Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class,
Aliases
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,
then: php artisan jwt:generate
Error:
[ReflectionException]
Method Tymon\JWTAuth\Commands\JWTGenerateCommand::handle() does not exist
The PR for this issue has been merged into develop
branch.
Try composer require tymon/jwt-auth:dev-develop --prefer-source
For Service Provider, you need to check docs
branch.
@Milos0110 public function handle() { $this->fire(); }
worked for me. Thanks!
I don't think messing with library is a good idea. Rather try
composer require tymon/jwt-auth:dev-develop --prefer-source
Delete jwt.php
(if previous version is installed) file from config folder and then try php artisan vendor:publish
. It'll list all the possible configurations to publish. You'll need to choose JWT.
and update provider in config/app.php
as
- If line
Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class
exists, update toTymon\JWTAuth\Providers\LaravelServiceProvider::class
- Else, add
Tymon\JWTAuth\Providers\LaravelServiceProvider::class
Then implement User
model with JWTSubject
and implement following functions:
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
Neither adding public function handle() { $this->fire(); }
nor doing composer require tymon/jwt-auth:dev-develop --prefer-source
worked for me. I'm on build 423
Using dev-develop
version worked for me on Laravel 5.5.4. The command for generating the key has changed from jwt:generate
to jwt:secret
.
@Onyekam after composer require tymon/jwt-auth:dev-develop --prefer-source
, delete jwt.php
file from config
folder and then try php artisan vendor:publish
. It'll list all the possible configurations to publish. You'll need to choose JWT.
Then follow the steps above. :)
public function handle() { $this->fire(); } this worked for me. Thanks @Milos0110 ๐
Why shouldnt you just rename fire() to handle()? I did this and all works fine.
I'm glad it helped!
handle() { $this->fire() } worked for me.
good job @Milos0110
@Milos0110 Thanks
Do what @tijanmdr suggested, just remember to remove the service provider and all aliases from app.php before trying to pull the latest version with composer. It works 100%.
Is anyone going to update the docs to reflect this??
thanks @Milos0110.
Why they didnt add this function? and require it!!
@Milos0110 excelent ๐ฏ . Create the handle() function then execute fire()
thanks @Milos0110
handle() { $this->fire() } worked for me. Did the Trick! Thanks buddy God bless you @Milos0110
thanks you.
jwt:generate ่ฐ็จ็ๅฝไปคๅฝๆฐhandle๏ผ๏ผๅจๆฐ็ๅทฒ็ปๆฒกๆไบ๏ผๅจๆฐ็ไธญ็ๅฝๆฐๆฏfire()๏ผๆไปฅไฟฎๆน vendor/tymon/jwt-auth/src/Commands/JWTGenerateCommand.phpๆไปถ๏ผ็ฑปไธญ้ดๆทปๅ ่ฏญๅฅpublic function handle()
{
$this->fire();
} ๅๆฌก่ฟ่กๅ่งฃๅณไธ่ฟฐ้ฎ้ขใ
I wrote this little command to rename all the fire() to handle(). After running it then the command works fine.
find ./vendor -type f \( -name '*Command.php' \) -exec grep -q 'fire()' {} \; ! -exec grep -q 'handle()' {} \; -print0 | xargs -0 sed -i 's@function fire(@function handle(@g'
The documentation is pretty clear. Head to the website for recent versions of Laravel. Also, use release candidate number 2 for Laravel 5.6.
There is also a nice tutorial for using this package here: https://blog.pusher.com/build-rest-api-laravel-api-resources/
Thanks, you solved my problem
It's frustrating to be starting another Laravel project and this still is on dev branch... ๐ญ
thx
As I don't like messing around in package source code and pull from develop branch confused me with so many different functions so I decided to create handle function to fire when you run php artisan jwt:generate
command
Run php artisan make:command NAME
and name it as you want - you can find it in App/Console/Commands then open it and in 'handle()' function paste this part of code $this->fire();
Replace extended class Command with this class use Tymon\JWTAuth\Commands\JWTGenerateCommand as Command;
and viola
@islam-moneer when I did so I got error The "show" option does not exist.
@Milos0110 thanks , this is worked for me
@aziev fix for "The "show" option does not exist." , in your new command file...
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'jwt:secret {--show}';
To work around this problem, create a class that is a default class:
Run command: php artisan make:command JWTGenerateCommand
Find to class JWTGenerateCommand (app/Console/Commands/JWTGenerateCommand):
namespace App\Console\Commands;
use \Tymon\JWTAuth\Commands\JWTGenerateCommand as Command;
/**
* Class Wrapper para sobrescrever o erro gerado no momento de executar o comando jwt:generate
* @link https://github.com/tymondesigns/jwt-auth/issues/1298
*
* Class JWTGenerateCommand
* @author David Pereira
* @package App\Console\Commands
*/
class JWTGenerateCommand extends Command
{
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Metodo chamado pelo framework no momento da execucao do comando
*/
public function handle()
{
$this->fire();
}
}`
``
After, update this file: app/Console/Kernel.php
Find to attribute $commands:
`protected $commands = [
\App\Console\Commands\JWTGenerateCommand::class,
];`
Save and execute command: php artisan key:generate
output: jwt-auth secret [1ih9dGbLsfCbvhWtaUYpmtH4gEQjE4wU] set successfully.
Same error on latest laravel. Working solution is
composer require tymon/jwt-auth:dev-develop --prefer-source
Which also includes auto discovery etc. Maybe one could create a new version because using dev-develop ist not a good way to install packages.
Go to JWTGenerateCommand.php and replace
public function fire()
to
public function handle()
It worked well for me.
It still a problem. We shouldn't need to change the JWTGenerateCommand.php
to get it working. Renaming the fire()
method to handle()
works fine, but it shouldn't be needed.
For the guys coming with newer versions of Laravel (I guess 5.5+), the issue is that by simply running
composer require tymon/jwt-auth
You'll obtain the 0.5-based version, that is not immediately compatible, interface-wise, with the latest versions of Laravel (that's why so many people says that renaming fire
by handle
did the job).
In order to fix that (assuming you already have installed the jwt library)
- Change the entry on your composer.json to
"tymon/jwt-auth": "^1.0"
- Run
composer update
- Run
php artisan vendor:publish
and provide the number for the JWT package (in my clean Laravel 5.7 install, it was the 8th option) - Then you can run
php artisan jwt:secret
as well
A bit more sweaty than modifying the library source, but avoids this bad practice (modify 3rd party code).
For the guys coming with newer versions of Laravel (I guess 5.5+), the issue is that by simply running
composer require tymon/jwt-auth
You'll obtain the 0.5-based version, that is not immediately compatible, interface-wise, with the latest versions of Laravel (that's why so many people says that renaming
fire
byhandle
did the job).In order to fix that (assuming you already have installed the jwt library)
- Change the entry on your composer.json to
"tymon/jwt-auth": "^1.0"
- Run
composer update
- Run
php artisan vendor:publish
and provide the number for the JWT package (in my clean Laravel 5.7 install, it was the 8th option)- Then you can run
php artisan jwt:secret
as wellA bit more sweaty than modifying the library source, but avoids this bad practice (modify 3rd party code).
This works, thank you. The problem is the version of the package that you get with "composer require tymon/jwt-auth"
Summary of working solution:
- composer require "tymon/jwt-auth": "^1.0"
- composer update
- php artisan vendor:publish (selection Tymon\JWTAuth\Providers\LaravelServiceProvider)
- php artisan jwt:secret
Advantage of this solution is you don't have to manualy add classes to config/app.php file
I think it's clear now that the newer version of the package is the way to go :)
So it's still not working? For version 1.0 it says: "Your requirements could not be resolved to an installable set of packages." means it's still under development?
which version of Laravel are you using @vedmant ? You have to be specific in what version you add in composer. I don't think it works if you just add ^1.0
For example if you're using Laravel 5.7 you have to add
"tymon/jwt-auth": "1.0.0-rc.3",
and if you're using 5.8 then add "tymon/jwt-auth": "1.0.0-rc.4.1",
edit: scratch that, just tested, "tymon/jwt-auth": "^1.0"
works fine and installs the latest release candidate.. at least it does on 5.8. If composer is telling you there's a problem with your deps, it's likely due with another package. Post your composer.json here and I can help
@vesper8 I use Laravel 5.7, I actually have following message: "The requested package tymon/jwt-auth ^1.0 is satisfiable by tymon/jwt-auth[1.0.0-alpha.2, 1.0.0-alpha.3, 1.0.0-alpha1, 1.0.0-beta.1, 1.0.0-beta.2, 1.0.0-beta.3, 1.0.0-rc.1, 1.0.0-rc.2, 1.0.0-rc.3, 1.0.0-rc.4, 1.0.0-rc.4.1, 1.0.x-dev] but these conflict with your requirements or minimum-stability."
Which means there is no stable 1.0 version? I don't what to change minimum stability to dev as it will affect other packages.
Ok, I found how to install it: composer require tymon/jwt-auth:^1.0@dev