I'm working on a REST API using JWT.
The login route is good, but the router in group middleware jwt.auth
doesn't work.
Error:
Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method Illuminate\Events\Dispatcher::fire()
My code:
Route::group(['namespace' => 'Api'], function(){
Route::get('user/login', 'AuthController@login');
Route::group(['middleware' => ['jwt.auth']], function() {
Route::get('auth/me', 'AuthController@me');
});
});
Well, i solved this error. them of 2hours..... :/
I modified the files Dispatcher.php of laravel.
1) first in the file vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php
1.1)Add this code.
/**
* Fire an event and call the listeners.
*
* @param string|object $event
* @param mixed $payload
* @param bool $halt
* @return array|null
*/
public function fire($event, $payload = [], $halt = false);
2) them in the second file. vendor\laravel\framework\src\Illuminate\Contracts\Events\Dispatcher.php
2.1)Add this code.
/**
* Fire an event and call the listeners.
*
* @param string|object $event
* @param mixed $payload
* @param bool $halt
* @return array|null
*/
public function fire($event, $payload = [], $halt = false)
{
return $this->dispatch($event, $payload, $halt);
}
fire
was changed to dispatch
in laravel 5.8 here https://github.com/laravel/framework/pull/26392
If you use tymondesigns/jwt-auth
package, this issue help you : https://github.com/tymondesigns/jwt-auth/issues/1787
This version fix this problem : https://github.com/tymondesigns/jwt-auth/releases/tag/1.0.0-rc.4
The Event
method fire
is deprecated in Laravel v5.8 However it works on prior version of Laravel v5.7 You can use the method dispatch
in Laravel v5.8 and later. It works same as fire
method and dispatches the event to event handler function registered in EventServiceProvider
JWT still using fire method and it's replced with dispatch method in Laravel 5.8 so the solution should change the method call in JWT.
this solution worked with me on Laravel 5.8
go to this path in your project
\vendor\tymon\jwt-auth\src\Middleware\GetUserFromToken.php
this is the original code:
public function handle($request, \Closure $next)
{
if (! $token = $this->auth->setRequest($request)->getToken()) {
return $this->respond('tymon.jwt.absent', 'token_not_provided', 400);
}
try {
$user = $this->auth->authenticate($token);
} catch (TokenExpiredException $e) {
return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
} catch (JWTException $e) {
return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
}
if (! $user) {
return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
}
$this->events->fire('tymon.jwt.valid', $user);
return $next($request);
}
and you should change:
$this->events->fire('tymon.jwt.valid', $user)
to:
$this->events->dispatch('tymon.jwt.valid', $user);
you only replace fire
method with dispatch
because fire
method is not available in Laravel 5.8 and the newest way is using dispatch
method.
I hope this solution working with you..
Changed fire() to dispatch() for Laravel 5.8+