Passport: Middleware Except does not work

Created on 23 May 2017  路  3Comments  路  Source: laravel/passport

  • Laravel Version: 5.4.0
  • PHP Version: 5.6.28
  • Database Driver & Version: mySQL

Description:

The exception from a middleware does not work anymore with mapApiRoutes() (and passport)

If i call api/v1/announcements i get:

{
  "error": "Unauthenticated."
}

even though it should be excepted from middleware auth:api

Steps To Reproduce:

Install laravel + passport, create a model (Announcement)

in RouteServiceProvider.php modify the _mapApiRoutes()_

protected function mapApiRoutes()
    {
        Route::prefix('api/v1')
             ->middleware(['auth:api','throttle:60','bindings'])
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

in your resource controller (AnnouncementController.php) add a _contructor_:

    /**
     * Instantiate a new AnnouncementController instance.
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
        // Even guests can view announcements
        if($request->ajax()) {
            $this->middleware('auth:api', ['except' => ['index','show']]);
        } else {
            $this->middleware('auth', ['except' => ['index','show']]);
        }
    }

in your routes/api.php add

Route::resource('announcements','AnnouncementController',['only' => [
    'index', 'show'
]]);

Most helpful comment

Already solved?My program still has this problem.

All 3 comments

Closing this issue because it's already solved, old or not relevant anymore. Feel free to reply if you're still experiencing this issue.

Already solved?My program still has this problem.

I'm having same issue on Laravel 5.4

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rudolfdobias picture rudolfdobias  路  3Comments

raksrivastava picture raksrivastava  路  3Comments

ghost picture ghost  路  3Comments

seriousjelly picture seriousjelly  路  3Comments

cookiejarblush picture cookiejarblush  路  4Comments