Skip to content
Advertisement

symfony 4: after successful authentication it redirects to admin area and populates the TokenStorage with an anonymous Token

I had an old Symfony 3.1 site that I upgraded to Symfony 3.4.x then to Symfony 4.4.11 but I didn’t upgrade it to symfony flex. I fixed many things and the public sites seem working.

I had to rebuild the authentication because the old one was not compatible with sf4.

I followed this https://symfony.com/doc/4.4/security/form_login_setup.html

and this: https://symfonycasts.com/screencast/symfony-security/make-user

I ended up in a situation that after a successful authentication when it redirects to the admin area then it always checks the LoginFormAuthenticator again which obviously doesn’t support the admin area and it redirects back to the login page with anonyous user.

There are many discussions about this issue and tried out all what I found but I didn’t find the solution. Not even with debugging it.

The session saved in the defined path. Its id is same like the PHPSESSID in the browser. Site runs HTTP protocol.

security.yml

JavaScript

routing:

JavaScript

User.php

JavaScript

backend controller:

JavaScript

LoginFormAuthentication.php

looks like the same in the example and it works. It successfully reaches the onAuthenticationSuccess() and redirects to the admin area.

dev.log

JavaScript

after the redirection:

JavaScript

Advertisement

Answer

my colleague figured out what is the problem. Actually there are multiple problems with the code above.

  1. using GuardAuthenticator inteface has been removed from sf4: https://github.com/symfony/symfony/blob/4.4/UPGRADE-4.0.md#security
  2. logout_on_user_change is not necessary
  3. no need of LoginFormAuthenticator.
  4. stateless: true is a wrong setting in the firewall but when I removed it then it throw a previous error: “Cannot refresh token because user has changed. Token was deauthenticated after trying to refresh it.” and it happened because
  5. in isEqualTo I checked the $this->salt !== $user->getSalt() but it was not serialized

so the working solution looks like this

  • the routing is the same
  • the backend controller is the same
  • LoginFormAuthentication.php was removed

security.yml

JavaScript

User.php

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement