public function socialLogin(array $data, array $options)
{
$reference = Hash::get($data, 'id');
$existingAccount = $this->_table->SocialAccounts->find()->where(['SocialAccounts.reference' => $reference, 'SocialAccounts.provider' => $data['provider']])->contain(['Users'])->first();
if (empty($existingAccount->user)) {
$user = $this->_createSocialUser($data, $options);
if (!empty($user->social_accounts[0])) {
$existingAccount = $user->social_accounts[0];
} else {
//@todo: what if we don't have a social account after createSocialUser?
throw new InvalidArgumentException(__d('CakeDC/Users', 'Unable to login user with reference {0}', $reference));
}
} else {
$user = $existingAccount->user;
}
if (!empty($existingAccount)) {
if ($existingAccount->active) {
if ($user->active) {
return $user;
} else {
throw new UserNotActiveException([$existingAccount->provider, $existingAccount->{$user}]);
}
} else {
throw new AccountNotActiveException([$existingAccount->provider, $existingAccount->reference]);
}
}
return false;
}