Skip to content

Instantly share code, notes, and snippets.

@mvpasarel
Last active June 17, 2016 19:02
Show Gist options
  • Select an option

  • Save mvpasarel/d1525449d17d1de0597bdc96ddb4f346 to your computer and use it in GitHub Desktop.

Select an option

Save mvpasarel/d1525449d17d1de0597bdc96ddb4f346 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Authenticate
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($guard) {
config(['auth.defaults.guard' => $guard]);
}
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
return $next($request);
}
}
<?php
namespace App\Illuminate\Auth;
use Illuminate\Auth\Access\Gate;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Auth\AuthServiceProvider as BaseAuthServiceProvider;
class AuthServiceProvider extends BaseAuthServiceProvider
{
/**
* @inheritdoc
*/
protected function registerAccessGate()
{
$this->app->singleton(GateContract::class, function ($app) {
return new Gate($app, function () use ($app) {
$guard = config('auth.defaults.guard');
return call_user_func($app['auth']->userResolver(), $guard);
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment