In a fully restful Laravel project, I'm using dingo/api
package. I need to set some configs and other stuff related to current user, before any request get handled. When we using dingo, we can access the current user like this:
$user = app('Dingo\Api\Auth\Auth')->user();
First I thought that I should do this in a service provider. But in there the Laravel has not yet initiated the dingo authenticating, therefore it's throw me an error. Then I thought I need to edit the dingo auth middleware called api.auth
to do this. Its Usage on my routes is like this:
<?php
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', ['prefix' => 'v1', 'namespace' => 'App\Http\Controllers'], function ($api) {
$api->group(['prefix' => 'admin', 'middleware' => 'api.auth'], function ($api) {
$api->get('checkRole/{branch_id}', 'RoleController@getRoles');
But I don't have any access to it because it's a builtin middleware in dingo package. So what should I do in this situation?
I solved this at its own time but I forgot to answer it here.
I simply extended the digo auth middleware and overrode its handle
method. But I had to replace the middleware name through all the route files in the project (I'm using nwidart/laravel-modules
package to write a modular project).
<?php
namespace App\Http\Middleware;
use Closure;
use Dingo\Api\Http\Middleware\Auth;
class ApiAuth extends Auth
{
public function handle($request, Closure $next)
{
$route = $this->router->getCurrentRoute();
if (!$this->auth->check(false))
$this->auth->authenticate($route->getAuthenticationProviders());
...
...
return $next($request);
}
}