search

Home  >  Q&A  >  body text

Laravel auth:api middleware does not work with newly created route files

<p>I have a web application using Laravel 7. On the application, I recently created a new api.php file under a folder called <code>api/v2</code> (<code>routes/api/v2/api.php< /code>). The problem I am facing is that in the newly created api.php file the 'auth:api' middleware is not working. </p> <p>I checked the request headers and found that the <code>authorization</code> header for these routes was empty. With the old routing files, everything works fine. </p> <p>I have registered the new api file in RouteServiceProvider.php as shown below</p> <pre class="brush:php;toolbar:false;">Route::prefix('api/v2') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api/v2/api.php'));</pre> <p>Except for the authentication issue, all routing is working fine without any issues. Since I cannot call the '$request->user()' method inside the controller. </p> <p>PS: I am using apache 2.4 </p> <p>Does anyone know why this is happening? </p>
P粉983021177P粉983021177448 days ago423

reply all(1)I'll reply

  • P粉466290133

    P粉4662901332023-09-02 11:47:34

    Can you share your route file? Here's what I tried to do that worked equally well:

    protected function mapAdminApiRoutes()
    {
        Route::prefix('api/admin')
             ->middleware(['api','jwt.verify','role:admin'])
             ->namespace($this->namespace.'\Admin')
             ->group(base_path('routes/admin-api.php'));
    }

    reply
    0
  • Cancelreply