Skip to content
Advertisement

symfony 5 authentification problem with getUser()

I am trying to work with Symfony 5 and the security component and I am blocked on a bug. the bug is on authentification success after redirection, $this->getUser() return null but at the same time, the development panel show me my role (which is store in database)

my security.yaml:

JavaScript

And I did not modify the User entity.

edit

Following advices, I added those lines in my security.yaml:

JavaScript

And the LoginFormAuthentificator file (sorry it will be long)

JavaScript

Thank you so much for help and time.

Advertisement

Answer

After some debugging, I found that you implement Serializable interface to your User class. With this approach, methods from this interface should returns correct data, because User object will be serialized to the session by these methods. In your case methods, serialize and unserialize were empty, which causing null value stored in session. So you should remove Serializable interface and it’s methods from your User class (in this case application store some default fields in session), or implement at least minimal functionality to this methods:

JavaScript

also, add return value to getUsername method implemented from UserInterface, e.g.

JavaScript

More information here and here

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