Newer Version Available

This content describes an older version of this product. View Latest

HeadlessSelfRegistrationHandler Interface

Creates customer and partner users during the Headless Registration Flow.

Namespace

Auth

Usage

The Headless Registration Flow allows you to control user registration experience in a third-party app while using Salesforce to authenticate users and manage their data access. When you set up this flow, add users in the class that is implementing the Auth.HeadlessSelfRegistrationHandler interface. This class runs after the user verifies their identity. For a detailed explanation of headless registration, see Headless Registration Flow for Private Clients or Headless Registration Flow for Public Clients, depending on your app type.

HeadlessSelfRegistrationHandler Methods

The following are methods for HeadlessSelfRegistrationHandler.

createUser(profileId, data, customUserDataMap, experienceId, password)

Returns a User object using information submitted by your off-platform app to Headless Registration API. The User object can be a new user that hasn’t been inserted in your org’s database, or it can represent an existing user record. If it’s a new User object, Salesforce inserts the user record for you.

Signature

public User createUser(Id profileId, Auth.UserData data, String customUserDataMap, String experienceId, String password)

Parameters

profileId
Type: Id
The ID of the profile that is assigned to new users.
data
Type: Auth.UserData
A class that stores information about the user, such as their name and locale.
customUserDataMap
Type: String
A string representation of a JSON object containing custom user information passed in during registration. We recommend that you deserialize this string into the equivalent Apex class structure. Determine what custom information to collect when you build your app’s registration experience.
experienceId
Type: String
A custom value that determines what the end user experiences.
password
Type: String
The user password.

Return Value

Type: User

HeadlessSelfRegistrationHandler Example Implementation

This example class implements the Auth.HeadlessSelfRegistrationHandler interface to create a user. It finds or creates an account to store the new user and creates a contact to associate with the account. It then creates the user based on information that your client sends to Headless Registration API.
1global class ExampleHeadlessReg implements Auth.HeadlessSelfRegistrationHandler {
2    // TO DO: Update this constant with the actual value for your use case
3    private static final String CUSTOMER_ACCOUNT = 'My Account';
4    
5    /*
6    * Retrieve an existing account or create a new one if it doesn't exist
7    *
8    * @param accountName - The name of the Account to find or create
9    * @return Account - The found or newly created Account record
10    */
11    private Account findOrCreateAccount(String accountName) {
12        List<Account> existingAccounts = [SELECT Id FROM Account WHERE Name=:accountName LIMIT 1];
13
14        if (existingAccounts.isEmpty()) {
15            Account newAccount = new Account(Name = accountName);
16            insert(newAccount);
17            return newAccount;
18        }
19
20        return existingAccounts[0];
21    }
22
23    /*
24    * Create a contact and associate it with an account
25    *
26    * @param account - The Account object to associate the contact with
27    * @param user - The User object containing the first and last name for the contact
28    * @return Contact - The newly created contact record
29    */
30    private Contact createContact(Account account, User user) {
31        Contact c = new Contact();
32        c.accountId = account.Id;
33        c.firstName = user.firstName;
34        c.lastName = user.lastName;
35
36        insert(c);
37
38        return c;
39    }
40
41    //TO DO: Implement any additional password validation that you want in this method. 
42    // In this example, the password was already checked to ensure that it complies with the org’s password policy,
43    // and the password, if present, is set automatically for the new user when they are returned from the createUserMethod.
44    private Boolean isPasswordValid(String password) {
45        return true;
46    }
47 
48
49    global User createUser(Id profileId, Auth.UserData data, String customUserDataMap, String experienceId, String password){
50        if (!isPasswordValid(password)) {
51            return null;
52        }
53
54        User u = new User();
55        u.Username = data.username;
56        u.ProfileId = profileId;
57        u.Email = data.email;
58        u.LastName = data.lastName;
59        u.FirstName = data.firstName;
60        String alias = data.username;
61        // Alias must be 8 characters or less
62        if (alias.length() > 8) {
63            alias = alias.substring(0, 8);
64        }
65        u.Alias = alias;
66        Account a = findOrCreateAccount(CUSTOMER_ACCOUNT);
67        Contact c = createContact(a, u);
68        u.ContactId = c.Id;
69        u.LanguageLocaleKey = UserInfo.getLocale();
70        u.LocaleSidKey = UserInfo.getLocale();
71        u.EmailEncodingKey = 'UTF-8';
72        u.TimeZoneSidKey = UserInfo.getTimezone().getID();
73
74        return u;
75    }
76}