mardi 14 juin 2016

Laravel login redirected you too many times

I have been struggling with this from quiet a time now, what i am trying is to redirect all the url's hit by non-logged in users to login page and it gives me this error, which I am sure is because it is creating a loop on /login URL. authentication is checking for authorized user in login page also. however I wish the login page should be an exception when checking the auth. I may be doing something wrong which I am not able to get. here goes my code.

routes.php

Route::post('login', 'Auth\AuthController@login');
Route::get('login' , 'Auth\AuthController@showLoginForm');
Route::get('/'     , 'Auth\AuthController@showLoginForm');


kernel.php

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'can' => \Illuminate\Foundation\Auth\Access\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'acl' => \App\Http\Middleware\CheckPermission::class,
];


Authenticate class

class Authenticate
{
    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');
        }
      }
    return $next($request);
    }
}


AuthController class

class AuthController extends Controller {

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    protected $redirectTo = '/dashboard';
    protected $loginPath = '/login';
    protected $redirectPath = '/dashboard';
public function __construct(){
    $this->middleware('auth', ['except' =>'login']); 
    /* I have been trying these many things to fix this, all in loss.
    // $this->middleware('acl'); // To all methods
    // $this->middleware('acl', ['only' => ['create', 'update']]); 
    // $this->middleware('guest', ['only' => ['/login']]);
    // echo "Fuck it"; exit;
    // $this->middleware('auth');
    // $this->middleware('auth', ['only' => ['login']]);
    // $this->middleware('auth', ['only' => ['/login']]);
    // $this->middleware('auth', ['except' => 'login']);
    // $this->middleware('guest');
    // $this->middleware('guest', ['only' => ['logout' , 'login', '/login', '/']]);
}

Please help me, It going all above my head, seems some sort of rocket science to me. well btw I am new to laravel and may be doing some silly thing around, apologies for that. Thanks in Advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire