Created
October 3, 2015 20:47
-
-
Save nicolaskempf57/20c1d2eb73a7c89d7f75 to your computer and use it in GitHub Desktop.
Authenticate Middleware for Lumen based on the Laravel one
This file contains 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\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 { | |
return redirect('auth/login'); | |
} | |
} | |
return $next($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment