Bruno Brito Bruno Brito - 1 year ago 621
PHP Question

Laravel 5.3 Auth:attempt is not working

Dont matter if i insert valid or invalid credentials, the user is always redirect to the route defined in RedirectIfAuthenticated middleware. My question: How can i redirect authenticated users to different route than non authenticated user?

RedirectIfAuthenticated (Middleware):

public function handle($request, Closure $next, $guard = null)
if (Auth::guard($guard)->check()) {
return redirect('/');

return $next($request);

This is my LoginController:

public function __construct()
$this->middleware('guest', ['except' => 'logout']);

public function authenticate(Request $data){
if (Auth::attempt( ['cnpj' => $data['cnpj'], 'password' => $data['password'] ] )){
return redirect()->route('someroute');
return redirect()->route('login');



Route::get('/', function () {
return view('welcome');
}) -> name('/');

Route::post('/register', 'Auth\RegisterController@create');

Route::get('/register', function () {
return view('auth.register');

Route::get('/login', function (){
return view('auth.login');

Route::post('/login', 'Auth\LoginController@authenticate');

Answer Source

In your LoginController, you have set the guest middleware to apply to all routes except for logout. That means that a user who is authenticated can never get to the authenticate method because the middleware has already been run and has redirected the user.

The simplest fix would be to add the authenticate method to the exclusions for the guest middleware:

public function __construct()
    $this->middleware('guest', ['except' => ['logout', 'authenticate']]);

It appears from your question that you don't really understand Laravel's concepts of Middleware, Routing, and Authentication. I highly recommend that you reread the documentation on those features, because it looks like you are trying to manually implement features that already exist in the framework.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download