I have a small dilema, i'm trying to make a login system that differentiates normal users from admin users using the laravel auth scaffolding.
The problem is it goes in a infinite redirect loop in the middleware.
After I press the login button it constantly redirects to a route and the question is, how can I solve this issue the "laravel way" or any other way for that matter.
Here are my controllers: 1. The basic home controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
}
The main admin controller - entry controller:
namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class Start extends Controller { public function index(){ return view('admin/index'); } }Login Controller(the default one from the auth scaffolding- modified by me, I removed the constructor):
namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; class LoginController extends Controller { use AuthenticatesUsers; protected $redirectTo = '/home'; }The Middleware(redirect if RedirectIfAuthenticated):
namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; class RedirectIfAuthenticated { public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { if(Auth::user()->type == 2){//if user type is 1 then it's an admin. return redirect()->route('web-admin'); }else{ return redirect()->route('home'); } } return $next($request); } }The route file(web routes)
Route::get('/', function () { return view('index'); }); Auth::routes(); Route::middleware(['auth','guest'])->group(function() { Route::get('home',['as'=>'home', 'uses'=>'HomeController@index']); Route::get('web-admin',['as'=>'web-admin', 'uses'=>'Admin\Start@index']); });