Sunday, 28 October 2018

Symfony 4 Remember Me doesn't work, the cookie is destroyed when browser reboot

I have an issue very similar to : Symfony Remember Me doesn't work, the cookie is destroyed when browser reboot

Unfortunately, their solution doesn’t quite fix it in Symfony 4.

After the connection, the cookie "REMEMBERME" is created. If I reboot my browser and I access on a page, the cookie is here (if I refresh he's destroyed) but I'm not connected.

I have been developing the authentication process as explained on the official documentation (to the book really, no fancy customization, no FOSUSERBUNDLE).

You can find my service.yaml built like in the documentation

security:
encoders:
    App\Entity\User:
        algorithm: bcrypt
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
    in_memory: { memory: ~ }
    our_db_provider:
        entity:
            class: App\Entity\User
            property: email
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        pattern:    ^/
        http_basic: ~
        provider: our_db_provider
        anonymous: ~
        form_login:
            login_path: login
            check_path: login
            default_target_path: dashboard
        remember_me:
            secret:   '%kernel.secret%'
            lifetime: 604800 # 1 week in seconds
            path:     /
            name:     REMEMBERME
            remember_me_parameter: _remember_me
        logout:
            path:  /logout
            target: /
    secured_area:
        form_login:
            csrf_token_generator: security.csrf.token_manager
            provider: our_db_provider
        logout:
            path:   /logout
            target: /

role_hierarchy:
        ROLE_ADMIN:      ROLE_USER
access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/reset_password,   role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/script_relationnal, roles: ROLE_ADMIN }
    - { path: ^/script_updates, roles: ROLE_ADMIN }
    - { path: ^/profile, roles: IS_AUTHENTICATED_REMEMBERED }
    - { path: ^/account, roles: ROLE_USER }

My login function is as detailed in the documentation as well

public function login(Request $request, AuthenticationUtils $authenticationUtils)
{
    // get the login error if there is one
    $error = $authenticationUtils->getLastAuthenticationError();

    // last username entered by the user
    $lastEmail = $authenticationUtils->getLastUsername();

    return $this->render('platform/user/login.html.twig', [
        'last_email' => $lastEmail,
        'error'         => $error,
    ]);
}

I have tried to replace ROLE_USER by IS_AUTHENTICATED_REMEMBERED in my routes (I don’t understand what exactly is the difference tho I read their doc about it) but nothing changed. The cookie still gets deleted when I close my browser

Any help here is much appreciated. I’d like to make the remember me feature work on my app. Many thanks.



from Symfony 4 Remember Me doesn't work, the cookie is destroyed when browser reboot

No comments:

Post a Comment