php - Symfony 5.3 - why is my remember me functionality not working?
Get the solution ↓↓↓I am just upgrading my symfony 4.4 application to 5.3 to use some new cool stuff (UX, UUID, ..). So I started a new project and ran the command to create the security components at latest defaults. Everything works perfect, except the remember me functionality. The cookie is just not set (regardless which browser). Maybe you can help me
security.yaml
{-code-2}
LoginFormAuthenticator
{-code-3}
login.html.twig
{-code-4}
Thanks a lot in advance!!
Answer
I originally asked this question for version 5.4, but is relevant for 5.3 as well - I tried both without getting the cookie to be set
Answer
Solution:
I setup a test app and confirmed the remember me cookie was not being sent but then I cheated and saw the hint on the Symfony Slack channel. When using the new passport based authentication system you need to use the remember me badge. It's documented here.
So update your Authenticator::authenticate method with:
return new Passport(
new UserBadge($email),
new PasswordCredentials($request->request->get('password', '')),
[
new CsrfTokenBadge('authenticate', $request->get('_csrf_token')),
new RememberMeBadge(),
]
);
It all seemed to work for me.
Answer
Solution:
I had same problem, cookie REMEMBERME was set but not called when PHPSESSID was deleted . After spending time I found in my User entity class this (I added after an upgrade of Symfony without thinking about it) :
public function getUserIdentifier() {
return $this->id;
}
But in secutiy.yml, my user provider is :
app_user_provider:
entity:
class: App\Entity\User
property: email
So I changed my getUserIdentifier function to this and it works now :
public function getUserIdentifier() {
return $this->email;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: attempt to read property "id" on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.