Slek33
Slek33

Reputation: 1

Remember me in Symfony2 not working after restart browser

The remember me functionality creating the REMEMBERME cookie fine. When I restart my browser that cookie is also available but I'm not logged and I'm authenticate like an anonymous in the Symfony Toolbar.

Normaly when I go on mydomain.com if I'm logged I'm redirect to mydomain.com/home/calendar if not I'm redirect to mydomain.com/login

#security.yml
main:
        pattern: ^/.+
        switch_user: { role: ROLE_SUPER_ADMIN, parameter: _steal_user }
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            always_use_default_target_path: true
            default_target_path: bleep_university_calendar
            login_path: /login
            check_path: /login_check
            remember_me: true
        remember_me:
            key: "%secret%"
            lifetime: 31536000
            always_remember_me: true
            remember_me_parameter: _remember_me
            path: /home/calendar
            domain: ~
        logout: true
        anonymous: false
        context: university

Can anyone explain me the reason for it's doesn't work ?

I'm using Symfony 2.1.7 and FOSuserbundle.

On Google Chrome the cookie REMEMBERME is never delete, I see it into the settings on all URL. But on Mozilla Firefox, if the attribute path under remember_me is / the cookie is deleting when I'm redirected on the URL /login. It's like the cookie is delete only on Firefox.

This is the value for REMEMBERME cookie on Firefox

expires : 1409988370
host : "localhost"      
isDomain : false        
isHttpOnly : true       
isSecure : false        
maxAge : undefined      
name : "REMEMBERME"     
path : "/"      
rawValue :  "QmxlZXBcTWFzdGVyU2Vydml...WJlOWI3ZTMyNzkxZGQyZGU3"     
value : "QmxlZXBcTWFzdGVyU2Vydml...WJlOWI3ZTMyNzkxZGQyZGU3"

Upvotes: 0

Views: 2529

Answers (2)

cure85
cure85

Reputation: 471

@nikolajosipovic is right.

Try to go to your User entity Provider e.g(AppBundle/Entity/User.php)

and add email to serialize()/unserialize() methods:

public function serialize()
{
    return serialize(array(
        $this->id,
        $this->username,
        $this->email, //Add email
        $this->password,
        $this->isActive,
        ));
}

public function unserialize($serialized)
{
    list(
        $this->id,
        $this->username,
        $this->email, //Add email
        $this->password,
        $this->isActive,
    ) = unserialize($serialized);

}

Upvotes: 1

nikolajosipovic
nikolajosipovic

Reputation: 106

If you are using another property than Username to authenticate the user, then the cookie might be saving the username while your script expects another property, like email.

You can override the default remember me service behavior. See this answer: https://stackoverflow.com/a/20550520/3096524

Upvotes: 3

Related Questions