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
-
Edit the
MainActivity
class so that it implementsNodeListener<FRUser>
:public class MainActivity extends AppCompatActivity implements NodeListener<FRUser> {
-
Add import statements for
org.forgerock.android.auth.NodeListener
andorg.forgerock.android.auth.Node
:import org.forgerock.android.auth.NodeListener; import org.forgerock.android.auth.Node;
-
At the bottom of the
MainActivity
class, add the handler methods from theNodeListener
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); } }
-
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(); }); } }
Check point
-
In Android Studio, select
.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
NameCallback
andPasswordCallback
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.