Easily add sign-in to your Android app with FirebaseUI

FirebaseUI is a library built on top of the Firebase Authentication SDK that provides drop-in UI flows for use in your app. FirebaseUI provides the following benefits:

  • Multiple Providers - sign-in flows for email/password, email link, phone authentication, Google Sign-In, Facebook Login, Twitter Login, and GitHub Login.
  • Account Management - flows to handle account management tasks, such as account creation and password resets.
  • Account Linking - flows to safely link user accounts across identity providers.
  • Anonymous User Upgrading - flows to safely upgrade anonymous users.
  • Custom Themes - customize the look of FirebaseUI to match your app. Also, because FirebaseUI is open source, you can fork the project and customize it exactly to your needs.
  • Smart Lock for Passwords - automatic integration with Smart Lock for Passwords for fast cross-device sign-in.

Before you begin

  1. Add Firebase to your Android project.

  2. Add the dependencies for FirebaseUI to your app-level build.gradle file. If you want to support sign-in with Facebook or Twitter, also include the Facebook and Twitter SDKs:

    dependencies {
        // ...
    
        implementation 'com.firebaseui:firebase-ui-auth:4.3.1'
    
        // Required only if Facebook login support is required
        // Find the latest Facebook SDK releases here: https://goo.gl/Ce5L94
        implementation 'com.facebook.android:facebook-android-sdk:4.x'
    
        // Required only if Twitter login support is required
        // Find the latest Twitter SDK releases here: https://goo.gl/E5wZvQ
        implementation 'com.twitter.sdk.android:twitter-core:3.x'
    }
    

    The FirebaseUI Auth SDK has transitive dependencies on the Firebase SDK and the Google Play services SDK.

  3. If you haven't yet connected your app to your Firebase project, do so from the Firebase console.

  4. In the Firebase console, open the Authentication section and enable the sign-in methods you want to support. Some sign-in methods require additional information, usually available in the service's developer console.

  5. If you support Google Sign-in and haven't yet specified your app's SHA-1 fingerprint, do so from the Settings page of the Firebase console. See Authenticating Your Client for details on how to get your app's SHA-1 fingerprint.

  6. If you support sign-in with Facebook or Twitter, add string resources to strings.xml that specify the identifying information required by each provider:

    
    <resources>
      <!-- Facebook application ID and custom URL scheme (app ID prefixed by 'fb'). -->
      <string name="facebook_application_id" translatable="false">YOUR_APP_ID</string>
      <string name="facebook_login_protocol_scheme" translatable="false">fbYOUR_APP_ID</string>
      <!-- Twitter consumer key and secret -->
      <string name="twitter_consumer_key" translatable="false">YOUR_CONSUMER_KEY</string>
      <string name="twitter_consumer_secret" translatable="false">YOUR_CONSUMER_SECRET</string>
    </resources>
    

Sign in

To kick off the FirebaseUI sign in flow, create a sign in intent with your preferred sign-in methods:

Java
Android

// Choose authentication providers
List<AuthUI.IdpConfig> providers = Arrays.asList(
        new AuthUI.IdpConfig.EmailBuilder().build(),
        new AuthUI.IdpConfig.PhoneBuilder().build(),
        new AuthUI.IdpConfig.GoogleBuilder().build(),
        new AuthUI.IdpConfig.FacebookBuilder().build(),
        new AuthUI.IdpConfig.TwitterBuilder().build());

// Create and launch sign-in intent
startActivityForResult(
        AuthUI.getInstance()
                .createSignInIntentBuilder()
                .setAvailableProviders(providers)
                .build(),
        RC_SIGN_IN);

Kotlin
Android

// Choose authentication providers
val providers = arrayListOf(
        AuthUI.IdpConfig.EmailBuilder().build(),
        AuthUI.IdpConfig.PhoneBuilder().build(),
        AuthUI.IdpConfig.GoogleBuilder().build(),
        AuthUI.IdpConfig.FacebookBuilder().build(),
        AuthUI.IdpConfig.TwitterBuilder().build())

// Create and launch sign-in intent
startActivityForResult(
        AuthUI.getInstance()
                .createSignInIntentBuilder()
                .setAvailableProviders(providers)
                .build(),
        RC_SIGN_IN)

When the sign-in flow is complete, you will receive the result in onActivityResult:

Java
Android

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);

        if (resultCode == RESULT_OK) {
            // Successfully signed in
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            // ...
        } else {
            // Sign in failed. If response is null the user canceled the
            // sign-in flow using the back button. Otherwise check
            // response.getError().getErrorCode() and handle the error.
            // ...
        }
    }
}

Kotlin
Android

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == RC_SIGN_IN) {
        val response = IdpResponse.fromResultIntent(data)

        if (resultCode == Activity.RESULT_OK) {
            // Successfully signed in
            val user = FirebaseAuth.getInstance().currentUser
            // ...
        } else {
            // Sign in failed. If response is null the user canceled the
            // sign-in flow using the back button. Otherwise check
            // response.getError().getErrorCode() and handle the error.
            // ...
        }
    }
}

Set up sign-in methods

  1. In the Firebase console, open the Authentication section. On the Sign in method tab, enable the Email/Password provider. Note that email/password sign-in must be enabled to use email link sign-in.

  2. In the same section, enable Email link (passwordless sign-in) sign-in method and click Save.

  3. You will also have to enable Firebase Dynamic Links to use email-link sign in. In the Firebase console, click on Dynamic Links under Grow in the navigation bar. Click on Getting started and add a domain. The domain you choose here will be reflected in the email links sent to your users.

  4. You can enable email link sign in in FirebaseUI by calling the enableEmailLinkSignIn on an EmailBuilder instance. You will also need to provide a valid ActionCodeSettings object with setHandleCodeInApp set to true. Additionally, you need to whitelist the URL you pass to setUrl, which can be done in the Firebase console, under Authentication -> Sign in Methods -> Authorized domains.

    ActionCodeSettings actionCodeSettings = ActionCodeSettings.newBuilder()
            .setAndroidPackageName(/* yourPackageName= */ ..., /* installIfNotAvailable= */ true,
            /* minimumVersion= */ null)
            .setHandleCodeInApp(true) // This must be set to true
            .setUrl("https://google.com") // This URL needs to be whitelisted
            .build();
    
    startActivityForResult(
            AuthUI.getInstance()
                    .createSignInIntentBuilder()
                    .setAvailableProviders(Arrays.asList(
                            new AuthUI.IdpConfig.EmailBuilder().enableEmailLinkSignIn()
                            .setActionCodeSettings(actionCodeSettings).build())
                    .build(),
            RC_SIGN_IN);
    
    
  5. If you want to catch the link in a specific activity, please follow the steps outlined here. Otherwise, the link will redirect to your launcher activity.

  6. Once you catch the deep link, you will need to call verify that we can handle it for you. If we can, you need to then pass it to us via setEmailLink.

    if (AuthUI.canHandleIntent(getIntent())) {
        if (getIntent().getExtras() == null) {
                return;
            }
            String link = getIntent().getExtras().getString(ExtraConstants.EMAIL_LINK_SIGN_IN);
            if (link != null) {
                startActivityForResult(
                        AuthUI.getInstance()
                                .createSignInIntentBuilder()
                                .setEmailLink(link)
                                .setAvailableProviders(getAvailableProviders())
                                .build(),
                        RC_SIGN_IN);
            }
    }
    
  7. Optional Cross device email link sign in is supported, which means that the link sent via your Android app can be used to log in on your web or iOS apps. By default, cross device support is enabled. You can disable it by calling setForceSameDevice on the EmailBuilder instance.

    See FirebaseUI-Web and FirebaseUI-iOS for more information.

Sign Out

FirebaseUI provides convenience methods to sign out of Firebase Authentication as well as all social identity providers:

Java
Android

AuthUI.getInstance()
        .signOut(this)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            public void onComplete(@NonNull Task<Void> task) {
                // ...
            }
        });

Kotlin
Android

AuthUI.getInstance()
        .signOut(this)
        .addOnCompleteListener {
            // ...
        }

You can also completely delete the user's account:

Java
Android

AuthUI.getInstance()
        .delete(this)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                // ...
            }
        });

Kotlin
Android

AuthUI.getInstance()
        .delete(this)
        .addOnCompleteListener {
            // ...
        }

Customization

By default FirebaseUI uses AppCompat for theming, which means it will naturally adopt the color scheme of your app. If you require further customization you can pass a theme and a logo to the sign-in Intent builder:

Java
Android

startActivityForResult(
        AuthUI.getInstance()
                .createSignInIntentBuilder()
                .setAvailableProviders(providers)
                .setLogo(R.drawable.my_great_logo)      // Set logo drawable
                .setTheme(R.style.MySuperAppTheme)      // Set theme
                .build(),
        RC_SIGN_IN);

Kotlin
Android

startActivityForResult(
        AuthUI.getInstance()
                .createSignInIntentBuilder()
                .setAvailableProviders(providers)
                .setLogo(R.drawable.my_great_logo)      // Set logo drawable
                .setTheme(R.style.MySuperAppTheme)      // Set theme
                .build(),
        RC_SIGN_IN)

You can also set a custom privacy policy and terms of service:

Java
Android

startActivityForResult(
        AuthUI.getInstance()
                .createSignInIntentBuilder()
                .setAvailableProviders(providers)
                .setTosAndPrivacyPolicyUrls(
                        "https://example.com/terms.html",
                        "https://example.com/privacy.html")
                .build(),
        RC_SIGN_IN);

Kotlin
Android

startActivityForResult(
        AuthUI.getInstance()
                .createSignInIntentBuilder()
                .setAvailableProviders(providers)
                .setTosAndPrivacyPolicyUrls(
                        "https://example.com/terms.html",
                        "https://example.com/privacy.html")
                .build(),
        RC_SIGN_IN)

Next Steps

  • For more information on using and customizing FirebaseUI, see the README file on GitHub.
  • If you find an issue in FirebaseUI and would like to report it, use the GitHub issue tracker.

Şunun hakkında geri bildirim gönderin...

Yardım mı gerekiyor? Destek sayfamızı ziyaret edin.