diff --git a/src/Model/Behavior/SocialBehavior.php b/src/Model/Behavior/SocialBehavior.php index 097955fa..94991a86 100644 --- a/src/Model/Behavior/SocialBehavior.php +++ b/src/Model/Behavior/SocialBehavior.php @@ -16,6 +16,7 @@ use Cake\Core\Configure; use Cake\Datasource\EntityInterface; use Cake\Event\EventDispatcherTrait; +use Cake\Log\Log; use Cake\ORM\Query; use Cake\Utility\Hash; use CakeDC\Users\Exception\AccountNotActiveException; @@ -77,13 +78,17 @@ public function initialize(array $config): void public function socialLogin(array $data, array $options) { $reference = $data['id'] ?? null; - $existingAccount = $this->_table->SocialAccounts->find() + $existingAccount = null; + + if ($reference) { + $existingAccount = $this->_table->SocialAccounts->find() ->where([ 'SocialAccounts.reference' => $reference, 'SocialAccounts.provider' => $data['provider'] ?? null, ]) ->contain(['Users']) ->first(); + } if (empty($existingAccount->user)) { $user = $this->_createSocialUser($data, $options); if (!empty($user->social_accounts[0])) { @@ -159,7 +164,12 @@ protected function _createSocialUser($data, $options = []) $this->_table->isValidateEmail = $validateEmail; - return $this->_table->save($user); + $savedUser = $this->_table->save($user); + if (!$savedUser) { + Log::debug('Unable save user. Errors: ' . json_encode($user->getErrors())); + } + + return $savedUser; } /**