Last active
June 17, 2016 19:02
-
-
Save mvpasarel/d1525449d17d1de0597bdc96ddb4f346 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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