You are here

public function LoginCookie__1_0::loginAndRespondWithCookie in RESTful 7.2

Login a user and return a JSON along with the authentication cookie.

Return value

array Array with the public fields populated.

File

src/Plugin/resource/LoginCookie__1_0.php, line 66
Contains \Drupal\restful\Plugin\resource\LoginCookie__1_0.

Class

LoginCookie__1_0
Class LoginCookie__1_0 @package Drupal\restful\Plugin\resource

Namespace

Drupal\restful\Plugin\resource

Code

public function loginAndRespondWithCookie() {

  // Login the user.
  $account = $this
    ->getAccount();
  $this
    ->loginUser($account);
  $user_resource = restful()
    ->getResourceManager()
    ->getPlugin('users:1.0');

  // User resource may be disabled.
  $output = $user_resource ? $user_resource
    ->view($account->uid) : array();
  if ($resource_field_collection = reset($output)) {

    /* @var $resource_field_collection \Drupal\restful\Plugin\resource\Field\ResourceFieldCollectionInterface */
    $resource_field_collection
      ->set('X-CSRF-Token', ResourceField::create(array(
      'public_name' => 'X-CSRF-Token',
      'callback' => '\\Drupal\\restful\\Plugin\\resource\\LoginCookie__1_0::getCSRFTokenValue',
    )));
  }
  return $output;
}