Open Danjavia opened 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. ? :)
Thanks I will to try this now, in the antoher hand, this affect the validate method of Request?
Hi @Danjavia
What do you mean, about its affecting the "Validate method of Request", do you mean this line
$request->ajax()
? :)
yeah!
@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);
}
}
Thanks I solved this issue using another library so many thanks!
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'); });
I try to acces to next route but launch the follow error
my code
Route::group([ 'middleware' => 'auth.user' ], function () { Route::get( '/dashboard', 'UserController@dashboard' ); });
kernel.php
AuthenticateUser class
public function handle($request, Closure $next) { if ( ! Auth::user()->get() ) { if ($request->ajax()) { return response('Unauthorized.', 401); } else { return redirect()->guest('api/v1/login'); } }
}