Kbwebs / MultiAuth

MIT License
138 stars 36 forks source link

Implement Auth middleware #10

Open Danjavia opened 9 years ago

Danjavia commented 9 years ago

I try to acces to next route but launch the follow error

Argument 1 passed to EasyCarga\Http\Middleware\AuthenticateUser::__construct() must implement interface Illuminate\Contracts\Auth\Guard, instance of Kbwebs\MultiAuth\AuthManager given

my code

// Authenticable routes

Route::group([ 'middleware' => 'auth.user' ], function () { Route::get( '/dashboard', 'UserController@dashboard' ); });

kasperbasse commented 9 years ago

Hi @Danjavia Will it work for you, if you just used this code:

if (Auth::user()->guest()) {
    if ($request->ajax()) {
        return response('Unauthorized.', 401);
    } else {
        return redirect()->guest('auth/login');
    }
}

return $next($request);

And remember to use: use Illuminate\Support\Facades\Auth; this at the top of the middleware.

If its not work like you expected, maybe i can see the code for that middleware, it looks like you are doing something with __contruct and what are you pulling into the middleware by using "use" at top of the middleware. ? :)

Danjavia commented 9 years ago

Thanks I will to try this now, in the antoher hand, this affect the validate method of Request?

kasperbasse commented 9 years ago

Hi @Danjavia What do you mean, about its affecting the "Validate method of Request", do you mean this line $request->ajax() ? :)

Danjavia commented 9 years ago

yeah!

ghost commented 8 years ago

@kasperbasse Another way to do, and make it work without using Facades and using dependency injection instead is to use the MultiManager and the Illuminate\Foundation\Application class to construct the MultiManager, per example :

<?php

namespace App\Http\Middleware;

use Closure;
use Kbwebs\MultiAuth\MultiManager;
use Illuminate\Foundation\Application;

class Authenticate
{
    /**
     * The MultiManager implementation
     *
     * @var \Kbwebs\MultiAuth\MultiManager
     */
    protected $auth;

    /**
     * @var \Illuminate\Foundation\Application
     */
    protected $app;

    /**
     * Authenticate constructor
     * @param \Illuminate\Foundation\Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
        $this->auth = new MultiManager($this->app);
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->admins()->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('/');
            }
        }

        return $next($request);
    }
}
Danjavia commented 8 years ago

Thanks I solved this issue using another library so many thanks!

andreaschrist27 commented 8 years ago

i have problem with middleware. this is my code `<?php

namespace App\Http\Middleware;

use Closure; use Illuminate\Contracts\Auth\Guard; use Session; 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 (Auth::user()->guest()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
          Session::flash('message-error','Kamu harus login dulu');
            return redirect()->guest('/');
        }
    }

    return $next($request);
}

} ` and routes.php

Route::group(['Middleware' => 'auth'], function () { Route::get('beranda','SPcontroller@beranda'); });