0

I have a third party login custom module that calls user_login_finalize() after authentication, and user_logout() upon logout. Both of these functionalities work except if the user logs in/out from the /user path in Drupal, the page loads the version of the /user page that should have rendered prior to login/logout. So when logging in, the page shows the unauthorized warning that was there at first, until the page is reloaded. When logging out, the page shows the user profile information that was there when the user was logged in.

This code runs in an event subscriber for KernelEvents::REQUEST (my organization's authentication works in a way that the status of whether a user is authenticated can change outside my Drupal site at any time and needs to be checked for before content is loaded).

I'm not sure if the problem has to do with the third party login redirecting, or Drupal's cache, but is there something else that needs to be cleared or reset after using these functions? I used them intentionally to avoid writing duplicate code from what's already in Drupal.

4
  • Sounds like a caching issue. Specifically the anonymous page cache. Try disabling that module and see if the problem goes away. If it does, you may need to set up some cache contexts, or even disable caching for that page.
    – Jaypan
    Commented Apr 18, 2022 at 2:38
  • Where did you put this code? Normally this runs in a controller or submit handler returning a redirect to build the correct page for the newly logged in or out user.
    – 4uk4
    Commented Apr 18, 2022 at 8:02
  • When users log out, are they still redirected to /user, or is the URL that appears in the browser address bar different but the rendered page is the user profile page? In the first case, it's the redirect that causes that; in the second case, it's a problem with the page cache.
    – avpaderno
    Commented Apr 18, 2022 at 8:38
  • I updated the description to address the comments/questions. In short, this is a custom module and I call the code in an event subscriber for KernelEvents::REQUEST. This is a caching issue of the /user page as far as I can tell. Is there a way to disable the anonymous user cache for just this page/route?
    – Anton
    Commented Apr 18, 2022 at 14:41

1 Answer 1

1

This code runs in an event subscriber for KernelEvents::REQUEST (my organization's authentication works in a way that the status of whether a user is authenticated can change outside my Drupal site at any time and needs to be checked for before content is loaded).

If the authentication status has changed then set a redirect response to built the new page after you have created a new session or destroyed the existing session via the user module functions you have mentioned:

$response = new RedirectResponse(Url::fromRoute('<current>')->toString());
$event->setResponse($response);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.