Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle null reference on social account creation #1045

Open
wants to merge 3 commits into
base: 13.next-cake4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Model/Behavior/SocialBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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])) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Loading