ForgeRock SDKs

Prepare for authentication

The sample application demonstrates user authentication. It shows the authentication status, and provides login and logout functionality.

  1. Open the activity_main.xml layout file, and add a TextView for the authentication status message, a login button, and a logout button:

    Blueprint of main app UI
    • The file to edit is app/src/main/res/layout/activity_main.xml.

    • The TextView has ID textViewUserStatus.

    • The Login button has ID buttonLogin.

    • The Logout button has ID buttonLogout.

  2. Link these items to the MainActivity class:

    private TextView status;
    private Button loginButton;
    private Button logoutButton;
    
    @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);
        // Temporary code to check that you have properly linked the buttons:
        String TAG = MainActivity.class.getName();
        loginButton.setOnClickListener(view -> Logger.debug(TAG, "Login button is pressed"));
        logoutButton.setOnClickListener(view -> Logger.debug(TAG, "Logout button is pressed"));
        // Method to add in the next step and to uncomment after you test the buttons:
        //updateStatus();
    }
  3. Add a method to update the authentication status when it changes:

    private void updateStatus() {
        runOnUiThread(() -> {
            if (FRUser.getCurrentUser() == null) {
                status.setText("User is not authenticated.");
                loginButton.setEnabled(true);
                logoutButton.setEnabled(false);
            } else {
                status.setText("User is authenticated.");
                loginButton.setEnabled(false);
                logoutButton.setEnabled(true);
            }
        });
    }

    After adding the method, uncomment its use in the onCreate() method.

Copyright © 2010-2022 ForgeRock, all rights reserved.