Last active
November 14, 2016 03:38
-
-
Save spicydog/436690cba9114fc471e001431943a3bd to your computer and use it in GitHub Desktop.
Laravel Authentication Middleware
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 (Auth::guard($guard)->guest()) { | |
if ($request->ajax() || $request->wantsJson()) { | |
return response('Unauthorized.', 401); | |
} else { | |
return redirect()->guest('login'); | |
} | |
} else { | |
if (! Auth::user()->isActive()) { | |
Auth::logout(); | |
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 | |
/* | |
|-------------------------------------------------------------------------- | |
| Application Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register all of the routes for an application. | |
| It's a breeze. Simply tell Laravel the URIs it should respond to | |
| and give it the controller to call when that URI is requested. | |
| | |
*/ | |
Route::auth(); | |
Route::group(['middleware' => 'auth'], function() | |
{ | |
// Whatever routes those need authentication put here, | |
// the guest user will be send to login page | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment