Score:1

How can I force user logout from a JavaScript library in a custom module?

in flag

I've written a custom module for Drupal 8 to monitor user device location frequently. If a user goes out of an allowed zone boundary, I need to force logging the user out of the website. I created following files in the module folder (gps_test):

  1. File gps_test.info.yml
  2. File gps.js: JavaScript program to monitor the user device's GPS location.
  3. File gps_test.libraries.yml: Contains the gps.js library.
  4. File gps_test.module: Implements a hook to attach the JavaScript library on every page load.
function gps_test_page_attachments(array &$page) {
  $page['#attached']['library'][] = 'gps_test/gps';
}

How can I force the user being logged out (or prevent the user from using the website) from the gps.js library?

Score:4
de flag

As @Tushar mentioned it, you can use the user_logout() function.

Simplest way I see is to log your user out with an AJAX call when your JS detects that the user needs to be logged out. Here is a basic example of how it can be done.

In your JS, you need to add:

Drupal.behaviors.logout= {
  attach: function(context, settings) {
    if(yourConditionToLogOutUserisMet) {
      $.ajax({
        url: "/log-me-out", // custom route to log current user out
        success: function(data) {
          // do whatever you want on success of your ajax call
        }
      });
    }
  }
}

Then, create your custom route in gps_test.routing.yml

gps_test.my_custom_log_out:
  path: '/log-me-out'
  defaults:
    _controller: '\Drupal\gps_test\Controller\MyLogOutController::logUserOut'
  requirements:
    _permission: 'access content'

And finally, create the route controller:

namespace Drupal\gps_test\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;


class MyLogOutController extends ControllerBase {
  function logUserOut() {
    if ($this->currentUser()->isAuthenticated()) {
      user_logout();

      return new JsonResponse([
        'message' => "Current user session has ended.",
      ], 200);
    }
  }  
}
leymannx avatar
ne flag
Drupal calls should be avoided in classes. Controllers have a `currentUser` method. `if ($this->currentUser()->isAuthenticated()) {`.
cn flag
There's also no `view published content` permission - you probably meant `access content`
misterdidi avatar
de flag
You are both right. I edited my answer to take your comments into account.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.