Reputation: 1599
Despite of all the other questions on stack-overflow I was not able to resolve the issue with all the provided information.
That's why I decided to create a new one.
So I am implementing the remember me function in my login form with a checkbox that looks the like this:
<input type="checkbox" id="form_saveCredentials" name="form[saveCredentials]">
On login everything looks fine and the cookie gets set correctly
Through the whole browser session the cookie remains alive.
But when I close the browser and reopen it the cookie is still there (did not navigate to my localhost yet!).
When I navigate to my website on localhost the cookie gets deleted according to the response header
I have no idea why the cookie gets deleted on the navigation to the website.
Maybe the problem lies in the securtiy.yml settings for the remember_me functionality
firewalls:
somefirewall:
form_login:
remember_me: true
remember_me:
key: %secret%
lifetime: 31536000
remember_me_parameter: 'form[saveCredentials]'
path: /
domain: ~
Any idea on how to fix this problem? Thanks in advance
Upvotes: 3
Views: 2409
Reputation: 1697
I had the same problem. On me was problem in App\Entity\User inside method getUserIdentifier() I returned fullname instead of login email
Upvotes: 0
Reputation: 129
I had the same problem but was my fault: in LoginFormAuthenticator::onAuthenticationSuccess() I did not return RedirectResponse
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
return new RedirectResponse($targetPath);
}
// my error : new RedirectResponse($this->urlGenerator->generate('my_route_to_page'));
return new RedirectResponse($this->urlGenerator->generate('my_route_to_page'));
//throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
}
Once changed, I can login with username or email and the browser remember the cookie when I reopen again.
Upvotes: 0
Reputation: 61
This problem can occur if your username is a valid e-mail but isn't equal to the e-mail. Remove any @ in usernames.
Upvotes: -2
Reputation: 158
I recently had this problem, and here is the solution I found:
The reason why the cookie is deleted is because the user information that the cookie contains doesn't match anything that Symfony knows about. It can't log a user in based on the cookie, so it just deletes it. The issue in my case was an incorrect string in my UserProvider class. The specific method is supportsClass
. It was returning 'AppBundle\Security\User'
when it should have actually been returning `'AppBundle\Entity\User'. This caused Symfony to be unable to find any users based on the information in the cookie, and then it would just delete the cookie and move on.
You can do some more troubleshooting if you go into this file: vendor/symfony/symfony/src/Symfony/Component/Security/Http/RememberMe/TokenBasedRememberMeServices.php
. Play around in processAutoLoginCookie
and see if you can't figure something out!
Good luck!
Upvotes: 7