Symfony 5.3.9 manually logging out logged in user and log in in another user

40 Views Asked by At

hello i am using symfony 5.3.9, what i want to achieve is when i click on a button(vue 3 as front end) i call a function
like this

public function __construct(TokenStorageInterface $tokenStorage, EventDispatcherInterface $eventDispatcher, SessionInterface $session, Security $security)
    {
        $this->tokenStorage = $tokenStorage;
        $this->eventDispatcher = $eventDispatcher;
        $this->session = $session;
        $this->security = $security;
    }



public function token(Request $request): JsonResponse
{
        $this->tokenStorage->setToken(null);
        $this->session->invalidate();
        $this->get('security.token_storage')->setToken(null);
        $user = $this->getUser();
        
        //show user by return or just var_dump

}

in the function token i am just trying to log out the user then the current user, $user should be null and it returns null, no problem there
the problem is if i call another end point and i return the user connected just after making the call for the token function, the old user still remains, for example

public function me(UserInterface $user = null): JsonResponse {
  $username = $user->getUsername();

return $username;
}

$user itself should be null here and give me an error, but the user itself it still filled with the old useri

please note that i have just put parts of the code here which i thought were appropriate so please forgive the "untidiness" of the code

I have tried

$this->tokenStorage->setToken(null);
$this->session->invalidate();
$this->get('security.token_storage')->setToken(null);

but the user never gets deleted.

1

There are 1 best solutions below

0
Oten On BEST ANSWER

Ok people. its just me .... i am using stateless firewall and jwt token and still trying to modify sessions... anyway just regenerating the jwt using the new user and sending it to the browser as part of -set-cookie header did the trick. Oh and the cookie has to be cleared first through else i will not be updated it ifs not yet expired. thanks.