ForgeRock SDKs 3.3

Enable authentication

This page describes how to:

  • Use the abstraction layer to start the authentication process and prepare to process nodes and callbacks.

  • Add a method to enable logout.

This page shows how to enable login and to perform logout. Your app will not yet collect credentials or process authentication callbacks.

In the following page, you will add the UI elements and code to collect the credentials, and to complete the authentication process.

Initiate authentication

  1. Call the FRUser.login() method to initiate authentication:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Logger.set(Logger.Level.DEBUG);
        FRAuth.start(this);
        status = findViewById(R.id.textViewUserStatus);
        loginButton = findViewById(R.id.buttonLogin);
        logoutButton = findViewById(R.id.buttonLogout);
        updateStatus();
        // Initiate authentication when the user presses the login button:
        loginButton.setOnClickListener(view -> FRUser.login(getApplicationContext(), this));
    }

    In this example, FRUser.login() initiates an authentication tree flow, and returns one of the following values:

    FRUser

    If the credentials already exist to authenticate the user session.

    Node

    If the authentication tree requires information from the user to continue the process.

    Error

    If an error occurs during the authentication flow.

    The first step in the authentication tree is to collect the username and password. Therefore, the first object returned here is a Node object that allows the authentication process to continue.

  2. Implement a NodeListener to handle node processing.

    The authentication process is iterative, and continues until it returns the FRUser object. The NodeListener interface helps you avoid repetitive code.

    Update the MainActivity class to implement NodeListener<FRUser>, and implement the methods to handle the result from FRUser.login():

    public class MainActivity extends AppCompatActivity implements NodeListener<FRUser> {
      // ...
    
      @Override
       public void onSuccess(FRUser result) {
         updateStatus();
       }
    
       @Override
       public void onCallbackReceived(Node node) {
         // Process the Node.
       }
    
       @Override
       public void onException(Exception e) {
         Logger.error(TAG, e.getMessage(), e);
       }
    }

    When the user presses the login button, the SDK initiates authentication, and passes the returned object to the NodeListener methods, which do the following:

    onSuccess()

    Complete the process when the FRUser object is returned.

    Note that the main thread must handle all UI changes.

    onCallbackReceived()

    Process the callbacks in the node.

    onException()

    Handle any errors.

    This sample application simply logs the exception.

Perform logout

  1. Call the FRUser.logout() method to end the user’s session:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Logger.set(Logger.Level.DEBUG);
        FRAuth.start(this);
        status = findViewById(R.id.textViewUserStatus);
        loginButton = findViewById(R.id.buttonLogin);
        logoutButton = findViewById(R.id.buttonLogout);
        updateStatus();
        loginButton.setOnClickListener(view -> FRUser.login(getApplicationContext(), this));
        // End the session when the user clicks the logout button:
        logoutButton.setOnClickListener(view -> {
            FRUser.getCurrentUser().logout();
            updateStatus();
        });
    }
  2. Run the application and click the login button.

    The application output shows the Node object returned.

Next, the application must process the nameCallback and the passwordCallback.

Copyright © 2010-2022 ForgeRock, all rights reserved.