It will be good to add event dispatcher as https://www.drupal.org/sandbox/gvso/2840522 module does. Or may be add this functionality at https://www.drupal.org/project/social_auth so that it can be easily reuse by other module. Please, Let me know your thoughts. I am also available for testing or any coding needed with proper guidance. What is a good way to get a hold on you for a quick question http://drupalslack.herokuapp.com or irc? Thanks for your good work.

Comments

nikathone created an issue. See original summary.

nikathone’s picture

Issue summary: View changes
gvso’s picture

Project: Social Auth Google » Social Auth
Version: » 8.x-1.x-dev
Related issues: +#2779641: Provide method to store user's picture

Moving issue to social auth.

I added #2779641: Provide method to store user's picture as a reference too

nikathone’s picture

Title: Add event subscriber as social auth facebook » Provide on login and create user event subscribers

Updated title for better description

nikathone’s picture

Title: Provide on login and create user event subscribers » Provide on login and create user event dispatchers
Issue summary: View changes

Oops should say event dispatchers

nikathone’s picture

Status: Active » Needs review
StatusFileSize
new8.71 KB

And here is a patch. I added a third event for the user fields are being initialized before creating the user entity. For example, the social auth facebook can use this to adds it's extras fields instead of using it's own create user. To test this patch you can add something like:

mymodule.user_created:
    class: Drupal\mymodule\EventSubscriber\UserCreatedSubscriber
    arguments: []
    tags:
      - { name: 'event_subscriber' }

In your mymodule.services.yml and inside UserCreatedSubscriber.php


<?php

namespace Drupal\mymodule\EventSubscriber;

use Drupal\social_auth\Event\SocialAuthEvent;
use Drupal\social_auth\Event\SocialAuthEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class UserCreatedSubscriber.
 */
class UserCreatedSubscriber implements EventSubscriberInterface {
  
  /**
   * Updates the user name
   * @param \Drupal\social_auth\Event\SocialAuthEvent $event
   */
  public function onUserCreated(SocialAuthEvent $event) {
    $user = $event->getUser();
    // adding a suffix on username to test
    $user->setUsername($user->getDisplayName() . 'bing');
    $user->save();
  }

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[SocialAuthEvents::USER_CREATED] = ['onUserCreated'];
    return $events;
  }

}


gvso’s picture

Status: Needs review » Needs work

Thanks @nikathone. This is an amazing job.

  1.  src/Event/SocialAuthEvent.php           | 41 +++++++++++++++++++++
    ...
    +class SocialAuthEvent extends Event {
    +  ¶
    ...
    +  protected $user;
    +  ¶
    ...
    +  public function __construct(User $user) {
    +    $this->user = $user;
    +  }
    +  ¶
    ...
    +  }
    +  ¶
    ...
    +final class SocialAuthEvents {
    +  ¶
    +  /**
    ...
    +  ¶
    +  /**
    +   * Name of the event fired when a new user is created via social auth.
    

    These are some of the many whitespaces in the patch.

  2. +++ b/src/Event/SocialAuthEvent.php
    @@ -0,0 +1,41 @@
    + * @see \Drupal\social_auth\Event\SocialAuthEvents
    + */
    +class SocialAuthEvent extends Event {
    

    Maybe we can rename the class to SocialAuthUserEvent

Isn't a way we can (optionally) pass the provider name to the events? For example Social Auth Google should pass the argument 'google', so Event Subscribers can use it to adapt their behavior.

nikathone’s picture

Status: Needs work » Needs review
StatusFileSize
new9.83 KB
new19.22 KB

Added a plugin_id argument to be passed for when creating or login user through social auth. If this get committed then we will have to update social_auth_google to do something like

<?php
$this->userManager->loginUser($drupal_user, 'social_auth_google');

$this->userManager->createUser($user->getName(), $user->getEmail(), 'social_auth_google');
?>

in it's code.

  • gvso committed 4217a99 on 8.x-1.x
    Issue #2843056 by nikathone: Provide on login and create user event...
gvso’s picture

Status: Needs review » Fixed
Related issues: +#2844049: Update loginUser and createUser calls in GoogleAuthController

Thank you very much. I've committed the patch with a few changes.

gvso’s picture

Social Auth Example was updated. We are only missing documentation for event handlers now

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.