Skip to content

Instantly share code, notes, and snippets.

@spicydog
Last active November 14, 2016 03:38
Show Gist options
  • Save spicydog/436690cba9114fc471e001431943a3bd to your computer and use it in GitHub Desktop.
Save spicydog/436690cba9114fc471e001431943a3bd to your computer and use it in GitHub Desktop.
Laravel Authentication Middleware
<?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);
}
}
<?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