You are here

function nodejs_auth_check in Node.js integration 8

Same name and namespace in other branches
  1. 6 nodejs.module \nodejs_auth_check()
  2. 7 nodejs.module \nodejs_auth_check()

Checks the given key to see if it matches a valid session.

1 call to nodejs_auth_check()
NodejsPages::messageHandler in src/Controller/NodejsPages.php
@todo .

File

./nodejs.module, line 477

Code

function nodejs_auth_check($message) {
  $nodejs_auth_check_callback = \Drupal::config('nodejs.config')
    ->get('auth_check_callback');
  if (!function_exists($nodejs_auth_check_callback)) {
    throw new Exception("No nodejs_auth_check callback found - looked for '{$nodejs_auth_check_callback}'.");
  }
  $uid = $nodejs_auth_check_callback($message['authToken']);
  $account = $uid > 0 ? \Drupal::service('entity_type.manager')
    ->getStorage('user')
    ->load($uid) : new AnonymousUserSession();
  $auth_user = new stdClass();
  $auth_user->uid = $account
    ->id();
  $auth_user->authToken = $message['authToken'];
  $auth_user->nodejsValidAuthToken = $uid !== FALSE;
  $auth_user->clientId = $message['clientId'];
  if ($auth_user->nodejsValidAuthToken) {

    // Get the list of channels I have access to.
    $auth_user->channels = array();
    foreach (\Drupal::moduleHandler()
      ->getImplementations('nodejs_user_channels') as $module) {
      $function = $module . '_nodejs_user_channels';
      foreach ($function($auth_user) as $channel) {
        $auth_user->channels[] = $channel;
      }
    }

    // Get the list of users who can see presence notifications about me.
    $auth_user->presenceUids = array_unique(\Drupal::moduleHandler()
      ->invokeAll('nodejs_user_presence_list', [
      $auth_user,
    ]));
    \Drupal::moduleHandler()
      ->alter('nodejs_auth_user', $auth_user);
    if ($auth_user->uid) {
      nodejs_user_set_online($auth_user->uid);
    }
    $auth_user->contentTokens = isset($message['contentTokens']) ? $message['contentTokens'] : array();
  }
  return $auth_user;
}