How to disable cache in my custom contextual filter code in drupal?

23 Views Asked by At

I have created a custom module for authentication by using access_token. The authentication is working fine. So, I thought to create a custom contextual filter and use that as a filter by access token. Please find my code below,

    <?php

namespace Drupal\simple_auth\Plugin\views\argument_default;

use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;

use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Site\Settings;

/**
 * Default argument plugin for the current user's carts.
 *
 * @ViewsArgumentDefault(
 *   id = "uid_from_simple_auth_access_token",
 *   title = @Translation("User ID from Simple Auth Acess Token")
 * )
 */
class UserFilterByAccessToken extends ArgumentDefaultPluginBase {

 /**
   * {@inheritdoc}
   */
  public function getArgument() {
    return $this->extractUserIdFromAccessToken();
  }

    /**
   * {@inheritdoc}
   */
  public function cacheTags() {
    return ['simple_auth_uri_uid_by_access_token'];
  }

  /**
   * Extracts the user ID from the access token.
   */
  protected function extractUserIdFromAccessToken() {
    $request = \Drupal::request();
    $access_token = $request->headers->get('access-token');

    if (!$access_token) {
      return new JsonResponse([
          "error" => "Access token is required"
      ], 401);
    }

    $storedRefreshToken = hash('sha256', $access_token . Settings::get('simple_auth_access_secret_key'));

    $query = \Drupal::database()->select('simple_auth_access_tokens', 'sart');
    $query->fields('sart', ['uid', 'expiration_time']);
    $query->condition('sart.token', $storedRefreshToken);
    $result = $query->execute()->fetchAssoc();

    return $result['uid'];
  }
}

The above code have created my contextual filter in the view. But my exact issue here is, for example,

  public function getArgument() {
    return 1;
  }

This filters the userid 1, when I check the view in browser (rest export view), and if I update the code like this

  public function getArgument() {
    return 2;
  }

I am getting the same result till clearing the cache. So I need to disable cache for this operation. I have tried many solutions but not getting what I have needed. I have tried adding cache tags and I cleared it using .module file, but it is not working.

Kindly help on this and if you need more info please reply.

0

There are 0 best solutions below