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
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'
]]);
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
Most helpful comment
Already solved?My program still has this problem.