In a decoupled application, when we rely on third party api to create user.
The create user flow has a minor change: Using social auth we first create user on third party tool and on success of that api call, we want to create user on Drupal and associate a customer id from that third party api to drupal user.
We have implemented this by subscribing to event SocialAuthEvents::USER_FIELDS. So, once we have the user fields from social auth provider, we make an api requests to third party api, But if third party api fails we throw an error in event subscriber. And when we throw an error it displays two error messages.

Here is what happens,
// \Drupal\social_auth\User\UserAuthenticator::authenticateUser
public function authenticateUser($name, $email, $provider_user_id, $token, $picture_url = FALSE, $data = '') {
.........
........
// At this point, create a new user.
$drupal_user = $this->userManager->createNewUser($name, $email, $provider_user_id, $token, $picture_url, $data);
$this->authenticateNewUser($drupal_user);
return $this->response;
}
while performing createNewUser, It calls - \Drupal\social_auth\User\UserManager::createUser and inside that method, There's a try..catch
try {
// Initializes the user fields.
$fields = $this->getUserFields($name, $email, $langcode);
........
........
return $new_user;
}
catch (\Exception $ex) {
$this->loggerFactory
->get($this->getPluginId())
->error('Could not create new user. Exception: @message', ['@message' => $ex->getMessage()]);
}
$this->messenger->addError($this->t('You could not be authenticated, please contact the administrator.'));
return FALSE;
that's why, when we throw error from getUserFields it will catch the error without creating user and shows first error message You could not be authenticated, please contact the administrator..
Then, as shown in the first code snippets it will call authenticateNewUser with $drupal_user Null
And as the $drupal_user arg is null, it will add another error message to display error message. You could not be authenticated. Contact site administrator.
public function authenticateNewUser(UserInterface $drupal_user = NULL) {
// If it's a valid Drupal user.
if ($drupal_user) {
// process based on drupal user object.
}
if (!$this->isRegistrationDisabled()) {
$this->messenger->addError($this->t('You could not be authenticated. Contact site administrator.'));
}
$this->nullifySessionKeys();
$this->response = $this->getLoginFormRedirection();
}
Hence we have two error messages here.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | 3146558-6.patch | 1.75 KB | miteshmap |
Comments
Comment #2
miteshmapComment #3
miteshmapComment #4
gvsoI think I understand the need for the other changes, but why is this needed?
Comment #5
miteshmap@gvso, That condition is triggering the second message for me. as attached in screenshot.
As we are checking this condition
if (!$this->isRegistrationDisabled()) {Which means, If registration is enabled and we are not able to login new user we display error message.Which is right !! I could see why we have this error messsage as you raised the question. I missed that while creating a patch. But, then how do we avoid two error messages?
I think we Should we add a check here -
if (!$this->isRegistrationDisabled() && $drupal_user) {Because, $drupal_user can be NULL and It could be null only if we have an error on - \Drupal\social_auth\User\UserManager::createUser Which means we already have an error message why $drupal_user does not hold an Object.Comment #6
miteshmapUpdated patch to remove error message and add a condition to display error message only if drupal user object is not null.
Comment #8
wells