Sakibul Alam
Sakibul Alam

Reputation: 1823

'auth' middleware in lumen throwing error

I am testing out lumen for the first time. Trying out auth middleware throws an error. I want to know whether such a middleware is shipped with lumen or do we need to implement our own? This is my routes file

$app->group(['middleware' => 'auth'], function ($app) {

    $app->get('/', ['as' => 'api', 'uses' => 'ApiController@index']);
});

and this is the error when trying to access the route

ErrorException in Manager.php line 137:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Auth\Guard' does not have a method 'handle'

Upvotes: 3

Views: 4824

Answers (2)

Konstantin L
Konstantin L

Reputation: 301

Actually all you need to do is to uncomment these lines in bootstrap/app.php

$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
]);

Upvotes: 2

krisanalfa
krisanalfa

Reputation: 6438

As you see, auth in Lumen container bound to Illuminate\Support\Manager\AuthManager. So, yeah, you have to create your own middleware. This is an example for your case.

Make your own middleware in app/Http/Middleware

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class Authenticate
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                // Lumen has no Redirector::guest(), this line is put the intended URL to a session like Redirector::guest() does
                app('session')->put('url.intended', app('url')->full());
                // Set your login URL here
                return redirect()->to('auth/login', 302);
            }
        }

        return $next($request);
    }
}

After this, bind your middleware in container. You can do this in bootstrap/app.php. Add these two lines before return.

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
    require __DIR__.'/../app/Http/routes.php';
});

$app->bind('App\Http\Middleware\Authenticate', 'App\Http\Middleware\Authenticate');
$app->alias('App\Http\Middleware\Authenticate', 'middleware.auth');

Now, instead of using auth in your middleware, use middleware.auth:

$app->group(['middleware' => 'middleware.auth'], function ($app) {
    $app->get('/', ['as' => 'api', 'uses' => 'ApiController@index']);
});

Upvotes: 5

Related Questions