'Symfony authentication (You cannot refresh a user from the EntityUserProvider)
After authentication , when I enter the right username/password I'm redirected to a new page ( driverspace/dashboard ) with this error :
You cannot refresh a user from the EntityUserProvider that does not contain an identifier.
The user object has to be serialized with its own identifier mapped by Doctrine.
And the symfony bar shows : "You are not authenticated" ..
Here my security.xml firewalls:
driver_firewall:
pattern: ^/driver
form_login:
provider: user_db
login_path: /driver/login
check_path: /driver/login_check
remember_me: true
always_use_default_target_path: false
default_target_path: /driverspace/dashboard
target_path_parameter: _target_path
use_referer: false
logout:
path: /driver/logout
target: /
remember_me:
key: MiPassphrase
lifetime: 1800
path: /.*
domain: ~
security: true
anonymous: true
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
logout: true
anonymous: true
PS : I have 2 Type of user , so i worked with fos to manage one and the basic authentication to manage the other , EDIT : Driver ENTITY
class Driver extends BaseUser implements \FOS\UserBundle\Model\UserInterface
{
/**
* @var integer
*
* @ORM\Column(name="id_driver", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idDriver;
/**
* @var string
*
* @ORM\Column(name="fname_drive", type="string", length=30, nullable=false)
*/
private $fnameDrive;
/**
* @var string
*
* @ORM\Column(name="lname_driver", type="string", length=30, nullable=false)
*/
private $lnameDriver;
/**
* @var string
*
* @ORM\Column(name="phone_driver", type="string", length=15, nullable=false)
*/
private $phoneDriver;
/**
* @var float
*
* @ORM\Column(name="lat_driver", type="float", precision=10, scale=0, nullable=true)
*/
private $latDriver;
/**
* @var float
*
* @ORM\Column(name="lon_driver", type="float", precision=10, scale=0, nullable=true)
*/
private $lonDriver;
/**
* @var integer
*
* @ORM\Column(name="activenow", type="integer", nullable=true)
*/
private $activenow;
/**
* @var \Company
*
* @ORM\ManyToOne(targetEntity="Company")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="company_id", referencedColumnName="id_company")
* })
*/
private $company;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Car", inversedBy="idDriver")
* @ORM\JoinTable(name="driver_car",
* joinColumns={
* @ORM\JoinColumn(name="id_driver", referencedColumnName="id_driver")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="id_car", referencedColumnName="id_car")
* }
* )
*/
private $idCar;
Solution 1:[1]
I solved the problem by :
1- Changing idDriver to id , because the driver class extends BaseUSer
2- adding :
public function serialize() {
return serialize($this->id);
}
public function unserialize($data) {
$this->id = unserialize($data);
}
I hope this solution will help someone
Solution 2:[2]
Adding this answer because it might help someone. You can also have this error after deleting a user and redirecting him to a login page. In this case, before redirecting, one should invalidate its session:
$request->getSession()->invalidate();
$this->tokenStorage->setToken(); // TokenStorageInterface
return $this->redirectToRoute('login');
Solution 3:[3]
This is for someone who uses Symfony 4 REST API and gets this error. This is because of the stateless flag being false in the security.yml and it needs to be true.
login:
pattern: ^/login
stateless: true
anonymous: true
json_login:
check_path: /login_check
success_handler: app.jwt_token_authenticator.success
failure_handler: app.jwt_token_authenticator.failure
Hope this helps someone, Cheers.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Asmaa |
| Solution 2 | |
| Solution 3 | Anjana Silva |
