0

i'm trying to remove the token property i have added in User Entity on logout listener but it's give me anonymous string. How can I get the User object to update it ?

My LogoutListener:

namespace App\Listener;

class LogoutListener implements LogoutHandlerInterface
{
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /**
     * This method is called by the LogoutListener when a user has requested
     * to be logged out. Usually, you would unset session variables, or remove
     * cookies, etc.
     */
    public function logout(Request $request, Response $response, TokenInterface $token)
    {
        var_dump($token->getUser());die;

        /*$user->setToken(null);
        $user->setDateToken(null);
        $this->entityManager->persist($user);
        $this->entityManager->flush();*/
    }
}

My service.yml:

    app.logout.listener:
        class: App\Listener\LogoutListener

My security.yml:

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login:
            pattern: ^/login
            anonymous: true
            json_login:
                  check_path: /login

        logout:
            pattern: ^/logout
            anonymous: true
            logout:
                path: /logout
                handlers: [app.logout.listener]
                success_handler: app.logout.success.listener
1
  • Have you tried inject Security service? You can get the user there... Commented Nov 17, 2019 at 16:50

2 Answers 2

1

My mistakes, I found the problem in my firewall config. The login pattern was wrong.

I finally replace it by :

        main:
            anonymous: ~
            json_login:
                check_path: /login
            logout:
                path: /logout
                handlers: [app.logout.listener]
                success_handler: app.logout.success.listener

So, it's not necessary to inject the security service. The token->getUser() works.

Sign up to request clarification or add additional context in comments.

Comments

0

On symfony 4, You can inject the security service on your construct and the call the getUser method in order to get it.

public function __construct(Security $security)
{
    $this->security = $security;
}

public function logout()
{
    $user = $this->security->getUser();
}

1 Comment

It's give me null user. It seems that user is logged out before handling the logout function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.