I am having problems with the logout_on_user_change
option set to true (which is default behavior in Symfony 4).
But only when running unit tests. In my dev environment everything (login, logout etc.) works fine.
But my unit tests which are trying to log in are failing. The authentication is not working anymore. I guess there must be something happening with the user in the background which leads to automatic logout due to this option.
I already updated the serialize()
method of the user entity, as suggested there: symfony 3.4 "Refreshing a deauthenticated user is deprecated"
Anyone having the same problem?
PROBLEM SOLVED: Found the solution myself: This lead myself in the right direction: Token was deauthenticated after trying to refresh it
Which is also important to note is that the checks in isEqualTo()
need to use the same fields as the serialize()
functions:
So in my case this works with logout_on_user_change
enabled:
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
return serialize(
array(
$this->id,
$this->email,
$this->password,
$this->salt
)
);
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list (
$this->id,
$this->email,
$this->password,
$this->salt
) = unserialize($serialized, ['allowed_classes' => false]);
}
public function isEqualTo(UserInterface $user)
{
if ($this->id !== $user->getId()) {
return false;
}
if ($this->password !== $user->getPassword()) {
return false;
}
if ($this->salt !== $user->getSalt()) {
return false;
}
if ($this->email !== $user->getEmail()) {
return false;
}
return true;
}