dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.33k stars 1.25k forks source link

How can I edit auth middleware? #1562

Closed amirhosseindz closed 6 years ago

amirhosseindz commented 6 years ago

I need to set some configs and other stuff related to current user, before any request get handled. If I'm correct 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:

  $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) {

But I don't have any access to it cause it's a builtin middleware. So what should I do in this situation?

gazben commented 6 years ago

@amirhosseindz Why do you want to do the stuff in the built in middleware? Why can't you do it a separate one? The preferred way of accessing the user is this: $request->user() In the middleware where you have the user.

amirhosseindz commented 6 years ago

I'm using nwidart/laravel-modules and I have a lot of modules in the project. So I don't want to edit each module and all of their routes to add a separate middleware. Thus I thought I could just edit that built in middleware which already used every where. Anyway, I found a solution myself. I extended that built in middleware class to my custom middlware class and did my stuff there. Then I set its alias api.auth to point to that custom middlware. I did this alias overriding in a service provider.