You are here

public function LinkedinAuthController::callback in Social Auth LinkedIn 8

Same name and namespace in other branches
  1. 8.2 src/Controller/LinkedInAuthController.php \Drupal\social_auth_linkedin\Controller\LinkedInAuthController::callback()
  2. 3.x src/Controller/LinkedInAuthController.php \Drupal\social_auth_linkedin\Controller\LinkedInAuthController::callback()

Callback function to login user.

1 string reference to 'LinkedinAuthController::callback'
social_auth_linkedin.routing.yml in ./social_auth_linkedin.routing.yml
social_auth_linkedin.routing.yml

File

src/Controller/LinkedinAuthController.php, line 117

Class

LinkedinAuthController
Manages requests to Linkedin API.

Namespace

Drupal\social_auth_linkedin\Controller

Code

public function callback() {
  $error = $this->request
    ->getCurrentRequest()
    ->get('error');
  if ($error) {
    drupal_set_message($this
      ->t('You could not be authenticated.'), 'error');
    return $this
      ->redirect('user.login');
  }

  /* @var \Linkedin\LinkedIn $client */
  $client = $this->networkManager
    ->createInstance('social_auth_linkedin')
    ->getSdk();
  $this->linkedinManager
    ->setClient($client)
    ->authenticate();

  // Saves access token so that event subscribers can call Linkedin API.
  $this->session
    ->set('social_auth_linkedin_access_token', $this->linkedinManager
    ->getAccessToken());

  // Gets user information.
  $user = $this->linkedinManager
    ->getUserInfo();

  // If user information could be retrieved.
  if ($user) {
    $picture = isset($user['pictureUrls']['values'][0]) ? $user['pictureUrls']['values'][0] : FALSE;
    $fullname = $user['firstName'] . ' ' . $user['lastName'];
    return $this->userManager
      ->authenticateUser($user['emailAddress'], $fullname, $user['id'], $picture);
  }
  drupal_set_message($this
    ->t('You could not be authenticated, please contact the administrator'), 'error');
  return $this
    ->redirect('user.login');
}