---
schema_version: "1"
id: 364466e5d3974998ea45189ace84ec45e6eba7a24c7a7fb75cee531403497e28
title: Customize the LoginDiscovery Handler
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/xcloud/264-0-0/external_identity_passwordless_login_customize_discovery_handler.md
canonical_url: https://help.salesforce.com/s/articleView?id=xcloud.external_identity_passwordless_login_customize_discovery_handler.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 2cc24eb715f09705148f2abf106bf93d735c6f9f5bd9a699af11669cd8ba60f3
description: After you create a Login Discovery page, continue setting up passwordless login with an email address or phone number by modifying the autogenerated Apex LoginDiscovery handler. By customizing the handler, you control the login process while Salesforce handles verification. The handler finds users based on the identifier that they provide on the login page, such as their email address. It then checks which verification methods the user has previously registered. The controller logs in the user by whichever method is configured.
products:
  - Security
  - Cross Cloud
release: "264"
---
# Customize the LoginDiscovery Handler

After you create a Login Discovery page, continue setting up passwordless login with an email address or phone number by modifying the autogenerated Apex LoginDiscovery handler. By customizing the handler, you control the login process while Salesforce handles verification. The handler finds users based on the identifier that they provide on the login page, such as their email address. It then checks which verification methods the user has previously registered. The controller logs in the user by whichever method is configured.

1. From Setup, in the Quick Find box, enter Apex Classes, and then select **Apex Classes**.
2. Find the autogenerated LoginDiscovery handler class. Its name begins with `AutocreatedDiscLoginHandler`, followed by a string of numbers.
3. Next to the LoginDiscovery handler, click **Edit**.
4. Use these methods to modify the generated code.
    
    `UserManagement.registerVerificationMethod`
    
    Registers an identity verification method, such as an email address or phone number. Only users can register verification methods. But you and users can deregister them.
    
    `UserManagement.deregisterVerificationMethod`
    
    Deregisters an existing identity verification method.
    
    `Auth.VerificationMethod` enum
    
    Represents the available verification methods, which include EMAIL, SALESFORCE\_AUTHENTICATOR, SMS, TOTP, and U2F.
    
    `UserManagement.formatPhoneNumber`
    
    Formats a mobile phone number to ensure that it’s in the format required by Salesforce. After formatting the phone number, update the mobile field of the user’s record with the output of `formatPhoneNumber`.
    
    `Site.passwordlessLogin`
    
    Logs in a user to a site using an identity verification method that the user previously registered.

For more information about these methods, see these links in the Apex Reference Guide.

- [LoginDiscoveryHandler Interface](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_Auth_LoginDiscoveryHandler.htm#apex_interface_Auth_LoginDiscoveryHandler)
- [UserManagement Class](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_UserManagement.htm)

## PasswordlessLogin Code Example

In this simple code snippet, an Apex controller contains the `passwordlessLogin` method. For a full implementation, see [LoginDiscoveryHandler Example Implemenation](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_Auth_LoginDiscoveryHandler.htm#apex_interface_Auth_LoginDiscoveryHandler_Example) in the Apex Reference Guide

```java
global with sharing class MFILoginController    
{
   //Input variables 
   global String input {get; set;}
   public String startURL  {get; set;}
   public List<Auth.VerificationMethod> methods;
   public String error;

   global MFILoginController()
   {
   }

   global PageReference login() 
   { 
        List<User> users = null;
        
        // Empty input
        if(input == null || input == '')
        {
            error = 'Enter Username';
            return null;
        }   
        users = [select name, id, email from User where username=:input];
        if(users == null || users.isEmpty())
        {
            error = 'Can\'t find a user';            
            return null;
        }  
        ID uid = users[0].id;
        if (startURL == null) startURL = '/';  
        
        // Check which verification methods the user has already registered 
        TwoFactorMethodsInfo AvailableMethods = [select 
            HasUserVerifiedMobileNumber,
            HasUserVerifiedEmailAddress,
            HasSalesforceAuthenticator,
            HasTotp, 
            HasU2F from TwoFactorMethodsInfo where userId=:uid];
        
        // If no verification method is available, prompt the user to enter a password
        // and invoke the site.login(usr,pwd) method
        // if(AvailableMethods.size() == 0)
                           
        // Add verification methods in priority order
        methods = new List<Auth.VerificationMethod>();
        if(AvailableMethods.HasUserVerifiedMobileNumber) 
            methods.add(Auth.VerificationMethod.SMS);
        if(AvailableMethods.HasUserVerifiedEmailAddress)
            methods.add(Auth.VerificationMethod.EMAIL);
        if(AvailableMethods.HasSalesforceAuthenticator)
            methods.add(Auth.VerificationMethod.
            SALESFORCE_AUTHENTICATOR);
        if(AvailableMethods.HasTotp)
            methods.add(Auth.VerificationMethod.U2F);
        if(AvailableMethods.HasU2F)
            methods.add(Auth.VerificationMethod.U2F);
          
        return Site.passwordlessLogin(uid, methods, startURL);
    }
}
```