Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Integrate Firebase User Authentication in Android App 11 Easy Steps

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 279
    Comment on it

    Firebase is a google's mobile platform that helps us to develop high-quality apps in minimum effort. Firebase provides Authentication library to authenticate users, you don't need to save user data on your local server because firebase saves that all.

     

    There are lots of ways to authenticate use like using Google login, phone authentication, email login, Facebook, twitter etc.

    Here we will work with google login, so follow the below steps to integration firebase authentication :

    1. Go to https://console.firebase.google.com and create a new project

    2. Now go to the side panel and click on Authentication option there you will see further options like email, phone, google etc, so click on google one.

    3. Click on enable button and save changes.

    4. Go to this link and set your SHA fingerprint here https://developers.google.com/android/guides/client-auth to generate SHA

    5. Go to your project settings and enter SHA and package name there to generate google services json file and after downloading that paste that in your project main folder.

    6. Now you are done with google console so add these dependencies in App Gradle file :

    implementation 'com.firebaseui:firebase-ui-auth:3.3.1'

     

    7. Go to your xml file and create a button for google login :

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fitsSystemWindows="true">
    
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="56dp"
                android:paddingLeft="24dp"
                android:paddingRight="24dp">
    
    
                <TextView
                    android:id="@+id/tvHeading"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="24dp"
                    android:text="Please login with existing account"
                    android:gravity="center"
                    android:textSize="20dip"/>
    
    
                <com.google.android.gms.common.SignInButton
                    android:id="@+id/btnLoginGmail"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:visibility="visible"
                    tools:visibility="visible" />
    
            </LinearLayout>
        </ScrollView>
    
    </layout>

     

    8. Here we are using data binding so directly access the view ids

    9. Firebase Auth class is the main class to perform authentication operation here so init in onCreate

    mAuth = FirebaseAuth.getInstance();

     

    10. Build Auth ui to process authentication in start activity

    AuthUI.getInstance() .createSignInIntentBuilder() .setAvailableProviders(providers) .build()

     

    11. Handle result callback in onActivityResult

    public class WelcomeActivity extends AppCompatActivity {
    
        private ActivityWelcomeBinding mBinding;
        private static final int RC_SIGN_IN = 123;
        private List<IdpConfig> providers = Arrays.asList(new IdpConfig.GoogleBuilder().build());
        private FirebaseAuth mAuth;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mAuth = FirebaseAuth.getInstance();
            mBinding = DataBindingUtil.setContentView(this, R.layout.activity_welcome);
            mBinding.btnLoginGmail.setOnClickListener(view -> signIn());
    
        }
    
        @Override
        public void onStart() {
            super.onStart();
            FirebaseUser currentUser = mAuth.getCurrentUser();
            //TODO move user to dashboard if user signed in
    
        }
    
        private void signIn(){
                startActivityForResult(
                        AuthUI.getInstance()
                                .createSignInIntentBuilder()
                                .setAvailableProviders(providers)
                                .build(),
                        RC_SIGN_IN);
    
        }
    
        @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) {
    
                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    
                    Toast.makeText(WelcomeActivity.this,  sSuccessLogin, Toast.LENGTH_LONG).show();
                }
            }
        }
    
    
    How to Integrate Firebase User Authentication in Android App  11 Easy Steps

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: