Skip to main content
Added code from pastebin referenced in comment
Source Link
metadaddy
  • 16.5k
  • 5
  • 57
  • 103

EDIT: Here is the Registration Handler code:

public class PortalHandler implements Auth.RegistrationHandler
{
    // createUser is called when there is no existing user linked 
    // to the incoming third party account
    public User createUser(Id portalId, Auth.UserData data)
    {
        User u;

        // Use incoming email for username, since we're working with a 
        // portal user
        List<User> l = [SELECT Id,UserName,FirstName,LastName,Email FROM User WHERE UserName = :data.email];
        if (l.size() > 0) 
        {
            u = l[0];
            System.debug('Found existing user record for '+ data.firstName + ' ' + data.lastName + ' ' + data.email);
            System.debug(u.FirstName + ' ' + u.LastName + ' ' + u.Email);
            
            // Update existing record
            u.Email = data.email;
            u.LastName = data.lastName;
            u.FirstName = data.firstName;
            // Useful to save the Facebook ID in a custom field
            //u.Facebook_ID__c = data.identifier;

            System.debug('Updating user record for '+ data.firstName + ' ' + data.lastName + ' ' + data.email);
            System.debug(u.FirstName + ' ' + u.LastName + ' ' + u.Email);
            
            update(u);
        } 
        else 
        {
            // Portal users need an associated contact, which, in turn,
            // needs to be associated with an account.
            // For simplicity, just put all contacts on the sForce account
            Account a = [SELECT Id FROM Account WHERE Name='Chapter ABC'];

            Contact c = new Contact();
            c.AccountId = a.Id;
            c.Email = data.email;
            c.FirstName = data.firstName;
            c.LastName = data.lastName;
            insert(c);

            u = new User();
            Profile p = [SELECT Id FROM profile WHERE name='Customer Community User'];

            u.UserName = data.email;
            u.Email = data.email;
            u.LastName = data.lastName;
            u.FirstName = data.firstName;
            //u.Facebook_ID__c = data.identifier;
            u.Alias = (data.username != null) ? data.username : data.identifier;
            
            if (u.Alias.length() > 8) 
            {
                u.Alias = u.Alias.substring(0, 8);
            }
            
            u.Languagelocalekey = UserInfo.getLocale();
            u.Localesidkey = UserInfo.getLocale();
            u.EmailEncodingKey = 'UTF-8';
            u.TimeZoneSidKey = 'America/Los_Angeles';
            u.ProfileId = p.Id;
            u.ContactId = c.Id;

            System.debug('Returning new user record for '+ data.firstName + ' ' + data.lastName + ' ' + data.email);

        }

        return u;
    }

    // updateUser is called when there is a match with an existing user
    public void updateUser(Id userId, Id portalId, Auth.UserData data)
    {
        User u = new User(Id=userId);

        u.Email = data.email;
        u.LastName = data.lastName;
        u.FirstName = data.firstName;
        //u.Facebook_ID__c = data.identifier;

        System.debug('Updating user record for '+ data.firstName + ' ' + data.lastName + ' ' + data.email);

        update(u);
    }
}

EDIT: Here is the Registration Handler code:

public class PortalHandler implements Auth.RegistrationHandler
{
    // createUser is called when there is no existing user linked 
    // to the incoming third party account
    public User createUser(Id portalId, Auth.UserData data)
    {
        User u;

        // Use incoming email for username, since we're working with a 
        // portal user
        List<User> l = [SELECT Id,UserName,FirstName,LastName,Email FROM User WHERE UserName = :data.email];
        if (l.size() > 0) 
        {
            u = l[0];
            System.debug('Found existing user record for '+ data.firstName + ' ' + data.lastName + ' ' + data.email);
            System.debug(u.FirstName + ' ' + u.LastName + ' ' + u.Email);
            
            // Update existing record
            u.Email = data.email;
            u.LastName = data.lastName;
            u.FirstName = data.firstName;
            // Useful to save the Facebook ID in a custom field
            //u.Facebook_ID__c = data.identifier;

            System.debug('Updating user record for '+ data.firstName + ' ' + data.lastName + ' ' + data.email);
            System.debug(u.FirstName + ' ' + u.LastName + ' ' + u.Email);
            
            update(u);
        } 
        else 
        {
            // Portal users need an associated contact, which, in turn,
            // needs to be associated with an account.
            // For simplicity, just put all contacts on the sForce account
            Account a = [SELECT Id FROM Account WHERE Name='Chapter ABC'];

            Contact c = new Contact();
            c.AccountId = a.Id;
            c.Email = data.email;
            c.FirstName = data.firstName;
            c.LastName = data.lastName;
            insert(c);

            u = new User();
            Profile p = [SELECT Id FROM profile WHERE name='Customer Community User'];

            u.UserName = data.email;
            u.Email = data.email;
            u.LastName = data.lastName;
            u.FirstName = data.firstName;
            //u.Facebook_ID__c = data.identifier;
            u.Alias = (data.username != null) ? data.username : data.identifier;
            
            if (u.Alias.length() > 8) 
            {
                u.Alias = u.Alias.substring(0, 8);
            }
            
            u.Languagelocalekey = UserInfo.getLocale();
            u.Localesidkey = UserInfo.getLocale();
            u.EmailEncodingKey = 'UTF-8';
            u.TimeZoneSidKey = 'America/Los_Angeles';
            u.ProfileId = p.Id;
            u.ContactId = c.Id;

            System.debug('Returning new user record for '+ data.firstName + ' ' + data.lastName + ' ' + data.email);

        }

        return u;
    }

    // updateUser is called when there is a match with an existing user
    public void updateUser(Id userId, Id portalId, Auth.UserData data)
    {
        User u = new User(Id=userId);

        u.Email = data.email;
        u.LastName = data.lastName;
        u.FirstName = data.firstName;
        //u.Facebook_ID__c = data.identifier;

        System.debug('Updating user record for '+ data.firstName + ' ' + data.lastName + ' ' + data.email);

        update(u);
    }
}
added 8 characters in body
Source Link
Samuel De Rycke
  • 9.6k
  • 8
  • 46
  • 73

After 3 and a half days of reading through documentation and trial and error, I have come to a point where I am just stuck trying to make it so Community Users and/or normal Users can log into a salesforce org by using their FaceBook login credentials.

Has anybody had success with this where they can help me out?

I thought I had set up the Auth Provider correctly in Salesforce as well a Registration Handler class and also an App in Facebook, but when I try and use it for a fictitious Facebook account that should be tied to a Community User license in Salesforce I get an error along the lines of:

There was a problem with your authentication attempt. Please try again. If you continue to encounter problems, contact your administrator.

There was a problem with your authentication attempt. Please try again. If you continue to encounter problems, contact your administrator.

I notice in the URL it has at the end "AuthorizationError?ErrorCode=NO_ACCESS&ErrorDescription=User+was+a+portal+user""AuthorizationError?ErrorCode=NO_ACCESS&ErrorDescription=User+was+a+portal+user".

Something tells me I am missing something really simple, but I am running out of things to try.

Any help would be greatly appreciated!

After 3 and a half days of reading through documentation and trial and error, I have come to a point where I am just stuck trying to make it so Community Users and/or normal Users can log into a salesforce org by using their FaceBook login credentials.

Has anybody had success with this where they can help me out?

I thought I had set up the Auth Provider correctly in Salesforce as well a Registration Handler class and also an App in Facebook, but when I try and use it for a fictitious Facebook account that should be tied to a Community User license in Salesforce I get an error along the lines of:

There was a problem with your authentication attempt. Please try again. If you continue to encounter problems, contact your administrator.

I notice in the URL it has at the end "AuthorizationError?ErrorCode=NO_ACCESS&ErrorDescription=User+was+a+portal+user".

Something tells me I am missing something really simple, but I am running out of things to try.

Any help would be greatly appreciated!

After 3 and a half days of reading through documentation and trial and error, I have come to a point where I am just stuck trying to make it so Community Users and/or normal Users can log into a salesforce org by using their FaceBook login credentials.

Has anybody had success with this where they can help me out?

I thought I had set up the Auth Provider correctly in Salesforce as well a Registration Handler class and also an App in Facebook, but when I try and use it for a fictitious Facebook account that should be tied to a Community User license in Salesforce I get an error along the lines of:

There was a problem with your authentication attempt. Please try again. If you continue to encounter problems, contact your administrator.

I notice in the URL it has at the end "AuthorizationError?ErrorCode=NO_ACCESS&ErrorDescription=User+was+a+portal+user".

Something tells me I am missing something really simple, but I am running out of things to try.

Any help would be greatly appreciated!

Source Link

Salesforce Auth Provider with Facebook SSO

After 3 and a half days of reading through documentation and trial and error, I have come to a point where I am just stuck trying to make it so Community Users and/or normal Users can log into a salesforce org by using their FaceBook login credentials.

Has anybody had success with this where they can help me out?

I thought I had set up the Auth Provider correctly in Salesforce as well a Registration Handler class and also an App in Facebook, but when I try and use it for a fictitious Facebook account that should be tied to a Community User license in Salesforce I get an error along the lines of:

There was a problem with your authentication attempt. Please try again. If you continue to encounter problems, contact your administrator.

I notice in the URL it has at the end "AuthorizationError?ErrorCode=NO_ACCESS&ErrorDescription=User+was+a+portal+user".

Something tells me I am missing something really simple, but I am running out of things to try.

Any help would be greatly appreciated!