ForgeRock Developer Experience

Step 5. Add login and logout calls

In this step, you update the app with the NodeListener interface, which manages the client side of the authentication journey.

The interface provides methods to handle the results of the authentication journey:

onSuccess()

The authentication journey is complete and an FRUser object is now available for further use.

For example, you could display the user’s name in your app.

onCallbackReceived()

Recursively handle each step within the authentication journey, by completing and returning any callbacks received.

For example, in this quick start guide we receive NameCallback and PasswordCallback callbacks. In the next step, we create the UI to request these credentials from the user.

onException()

Handle any errors.

Implement NodeListener and methods

  1. Edit the MainActivity class so that it implements NodeListener<FRUser>:

    public class MainActivity extends AppCompatActivity implements NodeListener<FRUser> {
  2. Add import statements for org.forgerock.android.auth.NodeListener and org.forgerock.android.auth.Node:

    import org.forgerock.android.auth.NodeListener;
    import org.forgerock.android.auth.Node;
  3. At the bottom of the MainActivity class, add the handler methods from the NodeListener interface:

    public class MainActivity extends AppCompatActivity implements NodeListener<FRUser> {
    
      // ...
      // ...
      // ...
    
      @Override
       public void onSuccess(FRUser result) {
         updateStatus();
       }
    
       @Override
       public void onCallbackReceived(Node node) {
         // Display appropriate UI to handle callbacks
       }
    
       @Override
       public void onException(Exception e) {
         Logger.error(TAG, e.getMessage(), e);
       }
    }
  4. Attach FRUser.login() and FRUser.logout() calls to the appropriate buttons, after the updateStatus() call:

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Logger.set(Logger.Level.DEBUG);
            FRAuth.start(this);
            // Add references to status view elements
            status = findViewById(R.id.textViewUserStatus);
            loginButton = findViewById(R.id.buttonLogin);
            logoutButton = findViewById(R.id.buttonLogout);
            updateStatus();
    
            // Attach `FRUser.login()` to `loginButton`
            loginButton.setOnClickListener(view -> FRUser.login(getApplicationContext(), this));
    
            // Attach `FRUser.getCurrentUser().logout()` to `logoutButton`
            logoutButton.setOnClickListener(view -> {
                FRUser.getCurrentUser().logout();
                updateStatus();
            });
        }
    }

Check point

  1. In Android Studio, select Run  Run 'app'.

    If everything is configured correctly, the app builds, and the emulator runs the application.

  2. In the Emulator, click the Log in button.

    In the Run pane, you should see the following to indicate that the journey was found and the callbacks were returned. In our case, a NameCallback and PasswordCallback callback, as configured in the page node:

    D/ForgeRock: [3.4.0] [AuthServiceResponseHandler]: Journey callback(s) received.

In the next step, you add a UI fragment to obtain credentials from the user, and code to open that fragment when the callback is received.

You also add code to populate the callback with the credentials and return it to the server, completing the authentication journey.

Copyright © 2010-2024 ForgeRock, all rights reserved.