public function JanrainCaptureApi::authenticate in Janrain Registration 8
Returns requested access token and set it to the current session.
Parameters
string $auth_code: The code of authentication.
string $redirect_uri: The URI to redirect to after successful call.
Return value
\Drupal\user\UserInterface The Drupal account of authenticated user.
Throws
\GuzzleHttp\Exception\GuzzleException
\Drupal\janrain_capture\Exception\JsonParseError
\Drupal\janrain_capture\Exception\JanrainApiCallError
\Drupal\Core\Entity\EntityStorageException
Overrides JanrainCaptureApiInterface::authenticate
File
- src/
JanrainCaptureApi.php, line 124
Class
- JanrainCaptureApi
- The integration between Janrain and Drupal.
Namespace
Drupal\janrain_captureCode
public function authenticate(string $auth_code, string $redirect_uri) : UserInterface {
$token = $this
->getToken(static::GRANT_TYPE_AUTHORIZATION_CODE, [
'code' => $auth_code,
'redirect_uri' => $redirect_uri,
]);
// Ideally, this method must not throw any exceptions here since
// we're using it with a newly requested access token. If it's
// untrue a user does not exist in Janrain.
$profile = $this
->getEntity($token);
$email = $profile
->getEmail();
// The UUID in Drupal and on Janrain should be the same.
$uuid = $profile
->getUuid();
// Check whether our application already knows a user.
$accounts = $this->userStorage
->getQuery('OR')
->condition('uuid', $uuid)
->condition('mail', $email)
->execute();
// This part will never be reached if a user doesn't exist on Janrain.
if (empty($accounts)) {
$is_new = TRUE;
$account = $this->userStorage
->create([
'uuid' => $uuid,
// The username must be unique as well as email and UUID.
'name' => $email,
'mail' => $email,
'status' => TRUE,
]);
$this->userStorage
->save($account);
}
else {
$is_new = FALSE;
/* @var \Drupal\user\UserInterface $account */
$account = $this->userStorage
->load(reset($accounts));
}
if ($account
->getAccountName() !== $email) {
$account
->setUsername($email);
$account
->save();
}
user_login_finalize($account);
// Update the current user account in memory. This needed to provide
// a correct user account for calls to "getAccessToken()" method in
// the same request.
$this->currentUser = $account;
// Ensure the user is marked as having a Janrain account.
$this->userData
->set('janrain_capture', $account
->id(), 'janrain_username', $profile
->getUsername());
// Inform subscribers about the successful authentication.
/* @see hook_janrain_capture_user_authenticated() */
$this->moduleHandler
->invokeAll('janrain_capture_user_authenticated', [
$profile,
$account,
$is_new,
]);
// Save the token to the database.
$this
->cache($token);
return $account;
}