Add JS file only when user is authenticated

Created on 9 January 2025, 11 days ago

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';
  }
}
Feature request
Status

Active

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024