Problem/Motivation

The JS always make requests to the routes even if user is not logged. This results in several requests returning 403 and being unneeded.

Proposed resolution

function autologout_attach_js(array &$element, array $settings) {
  if (\Drupal::currentUser()->isAuthenticated()){
    $element['#attached']['drupalSettings']['autologout'] = $settings;
    $element['#attached']['library'][] = 'autologout/drupal.autologout';
    $element['#cache']['tags'][] = 'config:autologout.settings';
  }
}

Issue fork autologout-3498529

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

emanuelcoelho created an issue. See original summary.

sandip’s picture

Assigned: Unassigned » sandip

Working on it.

sandip’s picture

Hi @emanuelcoelho,
I think we can check the authenticated user condition in function autologout_page_attachments_alter(array &$attachments) from where function autologout_attach_js(array &$element, array $settings) is called.
If you agree i will start working on it.

emanuelcoelho’s picture

Hi sandip,
Yes if possible.

sandip’s picture

Assigned: sandip » Unassigned
Status: Active » Needs review

Please review the MR

emanuelcoelho’s picture

Everything looks perfect.
For me it's a go.

Thank you for the quick response.

anish.ir’s picture

StatusFileSize
new174.89 KB
new45.68 KB

Hey @emanuelcoelho,

I have tried reproducing your issue but the libraries mentioned above are not being attached for anonymous users and is only being attached for authenticated users. So, I don't think we will need any validation here. I think the validation is already present for anonymous users.

// If user is anonymous.
  if ($user->id() == 0) {
    return TRUE;
  }

The above validation present in the autologout_autologout_prevent() method is validating for anonymous users.

Please provide detailed steps to replicate the issue, if I am missing something here.

I am using Drupal 11 and 2.x-dev version of the Module here. I am attaching screenshots for the reference.
Thanks !

anish.ir’s picture

Status: Needs review » Active
uttam’s picture

Status: Active » Needs review

I have tested the module, and I observed that the autologout.js file is not loading for anonymous users but is correctly loading for authenticated users. This behavior occurs without applying the proposed patch.

Since the reported issue was about autologout.js making unnecessary requests for anonymous users, but that is not happening in the 2.x-dev version, this might already be resolved.

Can anyone confirm if this issue still exists in any specific scenario?
Moving the issue to NR for now.

uttam’s picture

divyansh.gupta’s picture

Status: Needs review » Reviewed & tested by the community

Marking this as RTBC. The MR ensures that the JS file is only attached for authenticated users, as expected. It’s a straightforward change and has already been confirmed to work correctly by others. Looks good to go.

the_g_bomb’s picture

Agreed and confirmed. This appears to be a low-risk and straightforward change. It has a performance benefit and seems to work well.

inregards2pluto made their first commit to this issue’s fork.

inregards2pluto’s picture

StatusFileSize
new31.23 KB
new23.03 KB

Re-tested post https://www.drupal.org/project/autologout/issues/3339695 merge and can confirm behavior is still as expected. Not sure if due to other upstream changes in 2.x or due to patch, but it sounds like maintainers feel that its a low risk and straightforward change, so I'll trust the maintainer powers that be :)

the_g_bomb’s picture

Status: Reviewed & tested by the community » Fixed

Merged

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • the_g_bomb committed 46825537 on add_csrf_protection authored by sandip
    Issue #3498529: Add JS file only when user is authenticated
    

  • the_g_bomb committed 46825537 on 8.x-1.x authored by sandip
    Issue #3498529: Add JS file only when user is authenticated
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.