Create an Authentication Provider Registration Handler
Create an Apex registration handler to use with your authentication provider. When users
log in to your third-party app with an external single sign-on (SSO) provider, the
registration handler creates and updates their user records.
| Available in: both Salesforce Classic (not available in all orgs) and Lightning Experience |
| Available in: Enterprise, Unlimited, and Developer Editions |
- From Setup, in the Quick Find box, enter Apex, and then select Apex Classes.
- Click New.
- Fill the class with your registration handler code. You can paste in the code from the example.
- Save the class, and note its name.
Example
Here’s an example registration handler that you can use with your Google authentication provider. This class is triggered every time a user logs in with Google. If it’s the user’s first time logging in to your app, the class creates a user in Salesforce and associates them with the Headless Identity Demo profile that you created. The class also added the user to the account that you created earlier. If the user logged in to your app before, the class updates their record with any new information.
1/*
2 * This class is a sample auth provider registration handler for a Headless Identity implementation
3 * It creates an external user and associates them with the Headless Demo Profile
4 * It creates or associates the user contact to an Account called My Account
5 * It uses the email as the username
6 * */
7
8global class HeadlessDemoGoogleIDPRegistrationHandler implements Auth.RegistrationHandler{
9 static final String headless_account = 'My Account';
10 static final String headless_profile = 'Headless Demo Profile';
11
12 /*
13 * Tries to find a user with a username matching the incoming email
14 * If not it creates one, associates it to an Account and Profile
15 * */
16 global User createUser(Id portalId, Auth.UserData data) {
17 //Find an existing user, we are using username to map to email as your google email is a google username
18 List<User> users = [SELECT Id, firstName, lastName, email FROM User where Username =:data.email LIMIT 1];
19 User u = null;
20 if (!users.isEmpty()) {
21 u = users[0];
22 }
23 //If no user is found we create one
24 if (u == null) {
25 u = new User();
26 prepareUserData(data, u);
27
28 //Get the Account, and create it if one is not already present.
29 Account a;
30 List<Account> accounts = [SELECT Id FROM Account WHERE name='My Account'];
31 if(accounts.isEmpty()) {
32 a = new Account(name = headless_account);
33 insert(a);
34 } else {
35 a = accounts[0];
36 }
37
38 // Get the Profile
39 Profile p = [SELECT Id FROM Profile WHERE Name =: headless_profile LIMIT 1];
40
41 //Create the Contact
42 Contact c = new Contact();
43 c.accountId = a.Id;
44 c.firstName = u.firstName;
45 c.lastName = u.lastName;
46 insert(c);
47
48 //Associate the Contact to the user along with the profile.
49 u.profileId = p.Id;
50 u.contactId = c.Id;
51
52 } else {
53 u.firstName = data.firstName;
54 u.lastName = data.lastName;
55 u.email = data.email;
56 update u;
57 }
58
59 return u;
60
61 }
62
63 /*
64 * Basic Update User Method
65 * */
66 global void updateUser(Id userId, Id portalId, Auth.UserData data){
67 User u = new User(id=userId);
68 u.email = data.email;
69 u.lastName = data.lastName;
70 u.firstName = data.firstName;
71 update(u);
72 }
73
74 /*
75 * This method handles filling user data that is required by Salesforce but is not passed in during registration
76 */
77 void prepareUserData(Auth.UserData data, User u){
78
79 String name, firstName, lastName, username, alias, email;
80
81 System.debug('----> Passed In User Information');
82 System.debug('Email: ' + data.email);
83 System.debug('First Name: ' + data.firstName);
84 System.debug('Last Name: ' + data.lastName);
85
86 for(string key : data.attributeMap.keySet())
87 {
88 system.debug('key: ' + key + ' value: ' + data.attributeMap.get(key));
89 }
90 // Initialize the attributes essential for creating a new user with dummy values
91 // in case they will not be provided by the Auth Provider
92 firstName = 'change-me';
93 lastName = 'change-me';
94 email = 'change@me.com';
95 if(data.email != null && data.email != '')
96 email = data.email;
97 if(data.firstName != null && data.firstName != '')
98 firstName = data.firstName;
99 if(data.LastName != null && data.lastName != '')
100 lastName = data.lastName;
101 if(data.attributeMap.containsKey('full_name'))
102 name = data.attributeMap.get('full_name');
103 if(data.attributeMap.containsKey('name'))
104 name = data.attributeMap.get('name');
105 if(firstName == 'change-me' && name != '')
106 firstName = name.substringBefore(' ');
107 if(lastName == 'change-me' && name.substringAfter(' ') != '')
108 lastName = name.substringAfter(' ');
109
110 alias = firstName;
111
112 //Alias must be 8 characters or less
113 if(alias.length() > 8)
114 alias = alias.substring(0, 8);
115 u.username = email;
116 u.email = email;
117 u.lastName = lastName;
118 u.firstName = firstName;
119 u.alias = alias;
120 u.languagelocalekey = UserInfo.getLocale();
121 u.localesidkey = UserInfo.getLocale();
122 u.emailEncodingKey = 'UTF-8';
123 u.timeZoneSidKey = 'America/Los_Angeles';
124 }
125
126}