You are here

public function ApiClient::login in Rocket.Chat 8

Same name and namespace in other branches
  1. 8.2 modules/rocket_chat_api/src/RocketChat/ApiClient.php \Drupal\rocket_chat_api\RocketChat\ApiClient::login()

Do a Login on the Rocket Chat REST API.

Parameters

string $id: The Username.

string $token: The Authentication token. aka password.

Return value

bool Was the login successful or not.

File

modules/rocket_chat_api/src/RocketChat/ApiClient.php, line 143

Class

ApiClient
Class ApiClient.

Namespace

Drupal\rocket_chat_api\RocketChat

Code

public function login($id = NULL, $token = NULL) {
  $rocket = $this->config
    ->getElement('rocket_chat_url', "http://localhost:3000");
  $oldClient = $this->client;
  $this->client = $this
    ->createClient(TRUE);
  $params = [
    'username' => $id,
    'password' => $token,
  ];
  $result = $this
    ->postToRocketChat('login', [
    'json' => $params,
  ]);
  $validReturn = self::validateReturn($result);
  if (!$validReturn && $result['body']['status'] !== 'success') {
    $this->config
      ->notify("Login to {$rocket} was Unsuccessful.", 'error');
    unset($this->client);
    $this->client = $oldClient;
    return FALSE;
  }
  else {
    unset($oldClient);
    $this->config
      ->setElement("rocket_chat_uid", $result['body']['data']['userId']);
    unset($result['body']['data']['userId']);
    $this->config
      ->setElement("rocket_chat_uit", $result['body']['data']['authToken']);
    unset($result['body']['data']['authToken']);
    $this->config
      ->notify("Login to {$rocket} was Successful.", 'status');
    $this->client = $this
      ->createClient(FALSE);
    $this->loggedIn = TRUE;
    return TRUE;
  }
}