Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot logout with Symfony 4

I cannot logout a user.

I ported custom user management logic over to a Symfony 4 project. It uses recipes for security and guard.

Here is the logout config in my main firewall:

    logout:
        path: /logout
        target: /

Result: - User goes to /logout - User is redirected to / - is_granted("IS_AUTHENTICATED_REMEMBERED") continues to return true in my template (false is expected)

Other Considerations: - The firewall entry is getting triggered because I get errors if I remove it - I have tried adding additional parameters to logout to destroy the session and cookies, however that made no difference - Logging in works fine

Any idea on how to troubleshoot this?

:: edit - added security.yaml as requested ::

security:
    encoders:
        App\Entity\User: bcrypt
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN
    providers:
        app_users:
            entity: { class: App\Entity\User, property: email }
        app_oauth:
            id: app.oauth_user_provider
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            provider: app_users
            anonymous: ~
            oauth:
                resource_owners:
                    google: "/login/check-google"
                default_target_path: /
                login_path: /
                failure_path: /login
                oauth_user_provider:
                    service: app.oauth_user_provider
            remember_me:
                secret: "%env(APP_SECRET)%"
                lifetime: 2592000
                path:  /
            guard:
                authenticators:
                    - App\Security\LoginFormAuthenticator
                entry_point: App\Security\LoginFormAuthenticator
            logout:
                path: /logout
                target: /
            switch_user: ~
like image 797
Coder1 Avatar asked Dec 20 '17 17:12

Coder1


2 Answers

Add these commands

In security.yaml

logout:
                path:   /logout
                target: /
                invalidate_session: true

In controller

    /**
     * @Route("/logout", name="logout")
     */
    public function logout()
    {

    }

In logout button

<a class="text-muted" href="{{ path('logout') }}">logout </a>
like image 101
viveka Avatar answered Oct 23 '22 04:10

viveka


Check the serialize and unserialize methods for field $this->email in App\Entity\User.

like image 1
user9451958 Avatar answered Oct 23 '22 04:10

user9451958