AuthTabIntent


@ExperimentalAuthTab
public class AuthTabIntent


Class holding an Intent and other data necessary to start an Auth Tab Activity.

Create an instance of this class using the Builder and then call launch to present the auth page to the user.

Upon completion of the auth flow, the webpage should redirect to a URL with the custom scheme or an HTTPS URL with the host and path provided to the launch method.

Before starting the auth flow, you should create an ActivityResultLauncher using registerActivityResultLauncher. This launcher should be created unconditionally before every fragment or activity creation. The ActivityResultCallback provided to the launcher will be called with the result of the authentication flow, indicating success or failure.

If using an HTTPS redirect URL, you need to establish that your app and the redirect URL are owned by the same organization using Digital Asset Links. If the verification fails, the Auth Tab will return an AuthResult with the result code RESULT_VERIFICATION_FAILED.

Code sample:

// In your activity
private final ActivityResultLauncher<Intent> mLauncher =
            AuthTabIntent.registerActivityResultLauncher(this, this::handleAuthResult);

private void handleAuthResult(AuthTabIntent.AuthResult result) {
    // Check the result code
    boolean success = result.resultCode == AuthTabIntent.RESULT_OK;
    String message =
            getResources()
                    .getString(success ? R.string.auth_tab_success : R.string.auth_tab_failure);
    // Retrieve the result Uri
    message += " uri: " + result.resultUri;
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

...

private void launchAuthTab() {
    AuthTabIntent authTabIntent = new AuthTabIntent.Builder().build();
    authTabIntent.launch(mLauncher, Uri.parse("https://www.example.com/auth"), "myscheme");
}

...

Note: The constants below are public for the browser implementation's benefit. You are strongly encouraged to use AuthTabIntent.Builder.

Summary

Nested types

public final class AuthTabIntent.AuthResult

Class containing Auth Tab result data.

public final class AuthTabIntent.Builder

Builder class for AuthTabIntent objects.

Constants

static final String
EXTRA_HTTPS_REDIRECT_HOST = "androidx.browser.auth.extra.HTTPS_REDIRECT_HOST"

String extra that determines the host of the https redirect.

static final String
EXTRA_HTTPS_REDIRECT_PATH = "androidx.browser.auth.extra.HTTPS_REDIRECT_PATH"

String extra that determines the path of the https redirect.

static final String
EXTRA_LAUNCH_AUTH_TAB = "androidx.browser.auth.extra.LAUNCH_AUTH_TAB"

Boolean extra that triggers an Auth Tab launch.

static final String
EXTRA_REDIRECT_SCHEME = "androidx.browser.auth.extra.REDIRECT_SCHEME"

String extra that determines the redirect scheme.

static final int

Result code for when the Auth Tab is closed without the user completing the auth flow, e.g. the user clicked the close button.

static final int

Result code for when the Auth Tab is closed as a result of the expected redirect, implying the auth flow was completed.

static final int

Result code for when the Auth Tab implementation returns an invalid or unknown result.

static final int

Result code for when the Auth Tab is closed because the verification of the ownership of the HTTPS redirect URL failed.

static final int

Result code for when the Auth Tab is closed because the verification of the ownership of the HTTPS redirect URL couldn't be completed in a reasonable amount of time.

Public fields

final @NonNull Intent

An Intent used to start the Auth Tab Activity.

Public methods

static @NonNull AuthTabColorSchemeParams
getColorSchemeParams(
    @NonNull Intent intent,
    @IntRange(from = 1, to = 2) int colorScheme
)

Retrieves the instance of AuthTabColorSchemeParams from an Intent for a given color scheme.

@Nullable AuthTabSession.PendingSession
@Nullable AuthTabSession
boolean

Returns whether ephemeral browsing is enabled.

void
launch(
    @NonNull ActivityResultLauncher<Intent> launcher,
    @NonNull Uri url,
    @NonNull String redirectScheme
)

Launches an Auth Tab Activity.

void
launch(
    @NonNull ActivityResultLauncher<Intent> launcher,
    @NonNull Uri url,
    @NonNull String redirectHost,
    @NonNull String redirectPath
)

Launches an Auth Tab Activity.

static @NonNull ActivityResultLauncher<Intent>

Registers a request to launch an Auth Tab and returns an ActivityResultLauncher that can be used to launch it.

Constants

EXTRA_HTTPS_REDIRECT_HOST

Added in 1.9.0-alpha01
public static final String EXTRA_HTTPS_REDIRECT_HOST = "androidx.browser.auth.extra.HTTPS_REDIRECT_HOST"

String extra that determines the host of the https redirect.

EXTRA_HTTPS_REDIRECT_PATH

Added in 1.9.0-alpha01
public static final String EXTRA_HTTPS_REDIRECT_PATH = "androidx.browser.auth.extra.HTTPS_REDIRECT_PATH"

String extra that determines the path of the https redirect.

EXTRA_LAUNCH_AUTH_TAB

Added in 1.9.0-alpha01
public static final String EXTRA_LAUNCH_AUTH_TAB = "androidx.browser.auth.extra.LAUNCH_AUTH_TAB"

Boolean extra that triggers an Auth Tab launch.

EXTRA_REDIRECT_SCHEME

Added in 1.9.0-alpha01
public static final String EXTRA_REDIRECT_SCHEME = "androidx.browser.auth.extra.REDIRECT_SCHEME"

String extra that determines the redirect scheme.

RESULT_CANCELED

Added in 1.9.0-alpha01
public static final int RESULT_CANCELED = 0

Result code for when the Auth Tab is closed without the user completing the auth flow, e.g. the user clicked the close button.

RESULT_OK

Added in 1.9.0-alpha01
public static final int RESULT_OK = -1

Result code for when the Auth Tab is closed as a result of the expected redirect, implying the auth flow was completed.

RESULT_UNKNOWN_CODE

Added in 1.9.0-alpha01
public static final int RESULT_UNKNOWN_CODE = -2

Result code for when the Auth Tab implementation returns an invalid or unknown result.

RESULT_VERIFICATION_FAILED

Added in 1.9.0-alpha01
public static final int RESULT_VERIFICATION_FAILED = 2

Result code for when the Auth Tab is closed because the verification of the ownership of the HTTPS redirect URL failed.

RESULT_VERIFICATION_TIMED_OUT

Added in 1.9.0-alpha01
public static final int RESULT_VERIFICATION_TIMED_OUT = 3

Result code for when the Auth Tab is closed because the verification of the ownership of the HTTPS redirect URL couldn't be completed in a reasonable amount of time.

Public fields

intent

Added in 1.9.0-alpha01
public final @NonNull Intent intent

An Intent used to start the Auth Tab Activity.

Public methods

getColorSchemeParams

Added in 1.9.0-alpha01
public static @NonNull AuthTabColorSchemeParams getColorSchemeParams(
    @NonNull Intent intent,
    @IntRange(from = 1, to = 2) int colorScheme
)

Retrieves the instance of AuthTabColorSchemeParams from an Intent for a given color scheme.

Parameters
@NonNull Intent intent

Intent to retrieve the color scheme params from.

@IntRange(from = 1, to = 2) int colorScheme

A constant representing a color scheme. Must not be COLOR_SCHEME_SYSTEM.

Returns
@NonNull AuthTabColorSchemeParams

An instance of AuthTabColorSchemeParams with retrieved params.

getSession

Added in 1.9.0-alpha01
public @Nullable AuthTabSession getSession()

isEphemeralBrowsingEnabled

Added in 1.9.0-alpha01
@ExperimentalEphemeralBrowsing
public boolean isEphemeralBrowsingEnabled()

Returns whether ephemeral browsing is enabled.

launch

Added in 1.9.0-alpha01
public void launch(
    @NonNull ActivityResultLauncher<Intent> launcher,
    @NonNull Uri url,
    @NonNull String redirectScheme
)

Launches an Auth Tab Activity. Must be used for flows that result in a redirect with a custom scheme.

Parameters
@NonNull ActivityResultLauncher<Intent> launcher

The ActivityResultLauncher used to launch the Auth Tab. Use registerActivityResultLauncher to create this. See the class documentation for more details.

@NonNull Uri url

The url to load in the Auth Tab.

@NonNull String redirectScheme

The scheme of the resulting redirect.

launch

Added in 1.9.0-alpha01
public void launch(
    @NonNull ActivityResultLauncher<Intent> launcher,
    @NonNull Uri url,
    @NonNull String redirectHost,
    @NonNull String redirectPath
)

Launches an Auth Tab Activity. Must be used for flows that result in a redirect with the HTTPS scheme.

Parameters
@NonNull ActivityResultLauncher<Intent> launcher

The ActivityResultLauncher used to launch the Auth Tab. Use registerActivityResultLauncher to create this. See the class documentation for more details.

@NonNull Uri url

The url to load in the Auth Tab.

@NonNull String redirectHost

The host portion of the resulting https redirect.

@NonNull String redirectPath

The path portion of the resulting https redirect.

registerActivityResultLauncher

Added in 1.9.0-alpha01
public static @NonNull ActivityResultLauncher<IntentregisterActivityResultLauncher(
    @NonNull ActivityResultCaller caller,
    @NonNull ActivityResultCallback<AuthTabIntent.AuthResult> callback
)

Registers a request to launch an Auth Tab and returns an ActivityResultLauncher that can be used to launch it. Should be called unconditionally before the fragment or activity is created.

Returns
@NonNull ActivityResultLauncher<Intent>

An ActivityResultLauncher to be passed to launch.