Skip to content

Commit

Permalink
#manage-null-reference Handle null reference error for social users
Browse files Browse the repository at this point in the history
  • Loading branch information
steinkel committed Sep 7, 2023
1 parent 0d796dc commit 51579e2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Model/Behavior/SocialBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,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 @@ -151,7 +155,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

0 comments on commit 51579e2

Please sign in to comment.