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
FRUserobject 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
NameCallbackandPasswordCallbackcallbacks. In the next step, we create the UI to request these credentials from the user. onException()-
Handle any errors.
Implement NodeListener and methods
-
Edit the
MainActivityclass so that it implementsNodeListener<FRUser>:public class MainActivity extends AppCompatActivity implements NodeListener<FRUser> {java -
Add import statements for
org.forgerock.android.auth.NodeListenerandorg.forgerock.android.auth.Node:import org.forgerock.android.auth.NodeListener; import org.forgerock.android.auth.Node;java -
At the bottom of the
MainActivityclass, add the handler methods from theNodeListenerinterface: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); } }java -
Attach
FRUser.login()andFRUser.logout()calls to the appropriate buttons, after theupdateStatus()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(); }); }java
Check point
-
In Android Studio, select Run > Run 'app'.
If everything is configured correctly, the app builds, and the emulator runs the application.
-
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
NameCallbackandPasswordCallbackcallback, as configured in the page node:[4.8.3] [AuthServiceResponseHandler]: Journey callback(s) received.text
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.