function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sumanta Mukherjee 32Sumanta Mukherjee 32 

Facebook Authentication Issue in Salesforce Community

Hi all,

I am trying to login into Salesforce Customer Community using Facebook as Auth Provider. 

Issue faced: I am able to sign in to Salesforce community using facebook authentication, however I am not able to fetch the facebook user details such as firstname,lastname,email dynamically using the Registration Handler Class by  using data.firstname,data.lastname,data.email . Currently i am using static values for the fields like firstname,lastname,email 

Expected : Getting dynamic values from facebook in the code below where static values are provided.

Code for Registration Handler that i am using as below.

global class FacebookHandler implements Auth.RegistrationHandler{ 
    global User createUser(Id portalId, Auth.UserData data){
    system.debug('hahahahaha' +portalId);
      User result;
      try {
        //default account to collect Community Members
        Account a = [ SELECT Id
                      FROM Account
                      WHERE name='MyAllergy'
                      LIMIT 1
                  ];
      
       //default profile
      Profile p = [ SELECT Id
                     FROM Profile
                    WHERE name='Custom Customer Community Plus Login User'
                     LIMIT 1
                   ];
       //contact
       Contact c   = new Contact(
        accountId = a.Id,
         email     = 'prateek.sharma@salesforce.com',
         firstName = data.firstName,
         lastName = data.lastname

     //    lastName  = 'testr'
       );     
       insert c;
      //get the alias as username
      String alias = this.getAlias(data.username);
       //user
       User u = new User(
         username          = alias + '@community.com',                                    
         email             = 'prateek.sharma@salesforce.com',
        lastName          = 'testtr',
         firstName         = data.firstName,
        alias             = alias,
         languagelocalekey = 'en_US',
         localesidkey      = 'en_GB',
         emailEncodingKey  = 'UTF-8',
         timeZoneSidKey    = 'GMT',
        profileId         = p.Id,
         contactId         = c.Id
       );
       //binding
       result = u;
     }
     catch(Exception pEx) {
      //re-throwing exception to allow tracing the error from URL
      throw new HandlerException(
         pEx.getStackTraceString()
       );
     }
    return result;           
   }
   //custom exception
   global class HandlerException extends Exception{}
   global void updateUser(Id userId, Id portalId, Auth.UserData data){
        User u = new User(
       id        = userId,
      email     = 'prateek.sharma@salesforce.com',
       lastName  = 'tata',
     firstName = data.firstName,
     alias     = this.getAlias(data.username)
    );
    
     update u;
   }
   global String getAlias(String pUsername) {
     //generate random string (8)
     Blob blobKey           = crypto.generateAesKey(128);
     String key             = EncodingUtil.convertToHex(blobKey);
     String random_username = key.substring(0,8);
     //if not defined, create a random one
     String alias = pUsername != null ?
                     pUsername :
                     random_username;
     //check alias.length() > 8 and chop it
     alias = alias.length() > 8 ?
               alias.substring(0,8) :
               alias;
     return alias;
   }
}

Any help would be appreciated,

Thanks
sharathchandra thukkanisharathchandra thukkani
Probably you can get some information how to access the data after authenticating

https://developers.facebook.com
Michael WelburnMichael Welburn
Assuming you are leveraging the Facebook Authentication Provider in Salesforce, the values you reference should come directly from the data. Which scopes did you set for your Facebook app that you registered? What is the output if you debug the data variable upon registering?

https://help.salesforce.com/apex/HTViewHelpDoc?id=sso_provider_facebook.htm&language=en
Dheeraj_UpadhyayDheeraj_Upadhyay
Hello Sumanta Mukherjee 32

I'm using default Registration handler for facebook authentication.
below code may satisfy your needs.
 
global void updateUser(Id userId, Id portalId, Auth.UserData data){
  User u = new User(id=userId);
  //TODO: Customize the username. Must be 80 characters or less.
  //u.username = data.username + '@myorg.com';
  u.email = data.email;
  u.lastName = data.lastName;
  u.firstName = data.firstName;
  //String alias = data.username;
  //Alias must be 8 characters or less
  //if(alias.length() > 8) {
    //alias = alias.substring(0, 8);
  //}
  //u.alias = alias;
  update(u);
}



Let me know if that helps.
Thanks !!!