ssquare's avatar

Laravel got stuck after login. Laravel 5.8

I have done following:

Go to Login Page: amenity.local/login (or if I enter amenity.local it will redirect me to amenity.local/login) and after hitting enter the fav-icon shows loading forever and it got stuck there no results, no error, no timeout, nothing.

However, I could login with following cases:

  1. If I am already logged in and if I logged out. This will opens amenity.local/login page. Again, this time I could logged in easily without any hassle.

  2. If I am already logged in and then if I tried to access amenity.local/login then it will redirect to amenity.local/admin/property directly without showing login page.

But, As I have already mentioned, if I enter amenity.local/login in new page and enter credentials, then it stuck there forever. However, if I hit F5 (refresh) the page then it loads amenity.local/admin/property.

Login Controller

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/admin/property';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }


}

RedirectIfAuthenticated

class RedirectIfAuthenticated
{
    /**
     * 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)->check()) {
            return redirect('/admin/property');
        }
        return $next($request);
    }
}

Route

Auth::routes(['verify' => true]);

Route::get('/', function () {
    return redirect()->route('property.index');
});

Route::group(['middleware' => ['auth','verified']], function() {
    Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () {
    	Route::resource('property','PropertyController')->except(['create']);
    });
});  

route:list

| GET|HEAD | /  			 |   			  | Closure | web  
| GET|HEAD | admin/property  | property.index | App\Http\Controllers\Admin\PropertyController@index |verified   
| POST     | login 			 | 				  |App\Http\Controllers\Auth\LoginController@login | web,guest
0 likes
38 replies
marcosinghof's avatar

I have a very similar issue with 3 different Laravel projects. When I login there's about a 50% chance of getting stuck. Chrome tells me "processing request..." and I can let it run all day long -- nothing will happen. If I then reload the page it throws a CSRF token mismatch exception as the token has now already been used. Then I try to open the /login route again and I'm actually logged in.

It seems like the login works but the redirect does not. I have not been able to resolve this issue. I'm using the default Laravel auth behavior.

Also interesting, this happens on both my local machine with Apache and in production running on nginx.

shauncodes's avatar

I have a Laravel 6 project with default Laravel auth that does this in production on apache, but not on local apache. However, it does not throw a CSRF exception when you reload the page or click a the link in the navbar that loads a different page. Still horrible for user experience tho..

Snapey's avatar

Why do people mess with the redirectIfAuthenticated middleware

Its purpose is to stop you going to the login route when already logged in.

1 like
JonathanLilly's avatar

I had this issue and for the life of me could not find the fix until just now!

First, if the login is working but is stuck until the page is refreshed there is a break down with how you are redirecting.

We need to check a few things first: 1)check your "\vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php" and ensure you are redirecting to the proper place on the "authenticated" method

  1. go to "app\Http\Controllers\Auth\LoginController.php" and ensure your " protected $redirectTo" is redirecting to the place you want it to go

  2. This is probably where you were hung up

Go to : " vendor\laravel\framework\src\Illuminate\Foundation\Auth\RedirectsUsers.php "

In the redirectPath() method look for:

" return property_exists($this, 'redirectTo') ? $this->redirectTo : '/(YOUR REDIRECT PATH HERE)'; "

I am still new, but I had the exact same issue as you after messing with the routes trying to customize where I wanted to redirect to.

Best of luck!

Edit: I can't immediately figure out how to properly format this post and I am busy. I will try to remember to come back and format this. Sorry for the bad format.

blaab2's avatar

I have the same problem. I am still not sure how to fix it.

Peacemaker's avatar

Same here. Running on Apache2 on local and nginx on production. Infinite loading after posting login. Using Laravel 7 though

marlon25's avatar

I think the problem is the Google Chrome, when I tried other browsers it works fine and it redirected me to the dashboard automatically after login.

bratello's avatar

I have the same issue with LV 7.0. I use the Vuetify library on laradoc docker. I noticed that sometimes the login response returns the 302 redirect to js/rebuildFunctionalSlots.js.map uri, but sometimes it's stucks. I already enabled the log mail_driver since the smtp.mailtrap.io works very slow. The log channel itself very simple - just a file without network communication. Don't have idea how to trap this hang. According to the logs, the LoginController::authenticated() method passed OK, so probably it stacks somewhere in Login event listeners.

webdevmike's avatar

I have the same issue as well using Laravel 7 with Chrome. The first login doesn't redirect. Just stalls. If I refresh it then redirects the user to the proper logged in page. If I log out then all attempts to log back in work correctly. I'm so confused as to why.

orioltestart's avatar

Same here in 5.6 😢

I've tried to debug but its hard, its because it doesn't event throw an error

thomrockwell's avatar

I am having the same issue with Laravel 7 in Chrome in both local MAMP server, and production Ubuntu Linux server. Login works perfectly in Safari, but hangs indefinitely in Chrome. I have also discovered a second problem this morning! I have a page with the list of hyperlinks (passed from the controller as an array) that display as a simple unordered list. This page is not showing the links in Chrome, but is showing them fine in Safari on both local machine and production server. Need to get these Chrome issues resolved before I launch my site.

mboynes's avatar

Currently working through the same bug. I've also noticed it in Chrome, on an app using Laravel 7, only in two of my three profiles in Chrome. I've disabled all extensions in a profile and in incognito with no success. Here's something quite strange I've noticed: if I login in under 10 seconds, it fails (perpetual loading, nothing happens, etc.). If I wait more than 10 seconds to login, the login goes through without a hitch.

mboynes's avatar

Here's what I've gathered so far, in the hope that it helps someone else connect dots that they've observed:

  • I created a brand new app, used artisan ui:auth to scaffold the auth files, and have been able to replicate the issue (locally, using valet... however, I'm also experiencing this issue on a production site).
  • I can replicate the issue only in chrome so far (also tried safari and firefox). I have three profiles in chrome, for three separate google accounts. I can replicate it reliably in two of the three. In the third, I cannot replicate the issue. Further, in a "guest" profile I cannot replicate it. This suggests an extension to me, though I've disabled all extensions and restarted my browser. I haven't yet gone so far as to uninstall all extensions (just one, as noted below).
    • I saw it mentioned in GitHub that 1password/lastpass may be to blame. In both chrome profiles where this bug occurs, I did have 1password installed and in the third profile I did not. I've since gone so far as to first disable the extension then uninstall it, and the problem still occurs. If 1password were to blame, I would disabling/uninstalling the extension to fix it, but regardless, this seems like a good lead.
  • As mentioned in my last post, the issue only occurs if I sign in within ~10 seconds. If I wait more than 10 seconds after loading the page to sign in, it works. This, I feel, is an important clue, but I can't for the life of me do anything with it.
  • I've tried switching session storage to both cookies and database (from the default file), and the problem persists in each case.
  • As far as I can trace in the application, everything works perfectly and laravel attempts to send the response, which never arrives.
Norbertho's avatar

I have the same issue with a freshly installed Laravel app.. However i think it is somehow Chrome related because I dont experience this problem in Firefox.

jlrdw's avatar

I have not had to modify any out of the box middleware, I use the trait for redirects. Never had a problem with out of box Auth.

jlrdw's avatar

I just logged in, in chrome, but I use the authenticated method from the trait.

    public function authenticated(Request $request, $user)
    {
        
        $role = $user->role;
        $checkrole = explode(',', $role);
        if (in_array('admin', $checkrole)) {
            return redirect('admin/index');
            
        } else {
            return redirect('pet/index');
        }
    }

What happens if nothing is modified, and you only use the out of the box authentication, then can you login in Chrome?

Norbertho's avatar

i did not modify anything. this is happening with out of the box authentication..

jlrdw's avatar

@norbertho that is strange that mine works and yours don't. Could it be a setting in the browser, and do you have a fairly recent version of Chrome.

I have used laravel since version 4.2, and it's always worked in FF, Chrome, Ie, and now Edge.

I also use browser stack at times to test, but mostly mobile testing there.

You have seen this, https://github.com/laravel/framework/issues/32102 right.

mboynes's avatar

@jlrdw @norbertho this looks like it might be related to password extensions (lastpass, 1password). Did you see my notes above? Give it a read, you might find an explanation for why one of you experiences the issue and not the other.

Norbertho's avatar

@mboynes Hi, thanks for your answer. I dont use any extensions so in this case this could not be the problem . Problem must be somewhere else :/

Norbertho's avatar

@jlrdw yeah I saw that github issue page you just linked in. So as you can see I am not the only person who has this issue.. I understand that you dont experienced this problem before, but I havent changed my setup for a while and I disabled all my chrome extensions to track the issue. still now solution. Only think i can think of is that probably you are using a Mac and I am using a Win machine and maybe there are some differences, probably i will reinstall my whole system and see if it is solve this issue.

jlrdw's avatar

@norbertho I am also on Windows 10 2004 (latest version). Try just reinstalling chrome, but that is strange that a out of box auth isn't working for you. Is it latavel 7.* you have. Because I skipped several versions.

I went from version 5.1, next 5.5, next 5.5, 5.8, 6.* then updated to 7.*.

So try that new fresh chrome with a fresh install of 7.* laravel.

burakcalik's avatar

We have this issue on every single Laravel application from 5.x to 7.x.

Solution:

I've spend an hour and this is the only solution that I can find.

Add this to your LoginController.php

protected function authenticated(HttpRequest $request, $user)
{
    return view('loggedin');
}

Create resources/views/loggedin.blade.php with this content:

<script>
  window.location.href = "{{ App\Providers\RouteServiceProvider::HOME }}";
</script>

Thank me later.

2 likes
jungsid's avatar

We actually wrote 2 new Laravel application with 7.x recently and our users face the same issue with Login/Register first time after Chrome starts.

Apparently there is something wrong with the cookies as we notice that if we delete the cookies in Chrome, it backs to normal temporary.

battyus's avatar

This is not a Laravel issue, but rather it is a Chrome bug:

Read more at the following links:

Laravel is affected, because it is (correctly) using an HTTP 302 Redirect once a user is authenticated.

burakcalik's solution above is a good workaround, as it is using a javascript redirection, bypassing Laravel's own HTTP 302 redirection.

Another workaround is (as crazy as it sounds), to paste the password into the login form, instead of typing in.

Here is a demo how to reproduce 100% of the time: (I've built two simple php login forms to demonstrate - not Laravel code)

  • Visit http://test-domain-one.tk
  • MUST TYPE IN the credentials DO NOT COPY-PASTE:
  • TYPE in the email: [email protected]
  • TYPE in the password: LoginTest!
  • Once the form submitted, the server is using a 302 redirect
  • Chrome will offer you to save the credentials, save them
  • All good so far

Here is where the problem starts

  • Open a new tab
  • Visit the second login form at http://test-domain-two.tk
  • MUST TYPE IN the exact same credentials DO NOT COPY-PASTE:
  • TYPE in the email: [email protected]
  • TYPE in the password: LoginTest!
  • Once the form submitted, the server is using a 302 redirect
  • Chrome will hung indefinitely

If you'd like, click on the Login button again, and you'll be logged in, Chrome will offer to save the credentials and everything will work fine on both domains.

If you'd like to reproduce the issue again, then Clear all Browsing Data between the trials. (CTRL SHIFT DEL, Clear Data)

2 likes
battyus's avatar

It seems like that in Chrome Version 85.0.4183.83 the issue has been fixed.

bar1024's avatar

I just updated Chrome to version 85.0.4183.83 and I am still experiencing this issue.

battyus's avatar

Unfortunately it is true, the issue still pops up in Chrome Version 85.0.4183.83 but much less often and currently can not find a way to reproduce it consistently.

Next

Please or to participate in this conversation.