Well, if minimal is what you like best then it is for you. Without much ado let's start to add Firebase authentication to your app.
Firebase Authentication is the best quick authentication technique used to login users to an Android app. It is very easy to set up and requires a very minimal amount of coding. It only has one disadvantage. It does not allow you to edit the login screen. You can only put logo with privacy settings and Terms and conditions link on your login screen. Well, if minimal is what you like best then it is for you. Without much ado let's start to add Firebase authentication to your app.
[post_ads]
Now Sync your Gradle. Add facebook dependency only if you want to add facebook login to your app. Now import this statements in your main.activity.
Now go to Firebase Console and make a new project. Enter the name of your project, tick all the boxes and click on Accept.
Click on Android Icon to add your android app to your project.
Enter the details of your android app by entering details like app name, package name and SHA1 signature.
You can find a SHA1 signature in signing the report of your app.
[post_ads_2]
Once you click on register you will be asked to download google-services.json. Paste it in the root directory of your app.
Initialize this variable in your main activity class.
In your onCreate() method initialize firebase.
In your Main Activity class, create a signInIntent.
In .setlogo you can set your logo for showing logo on your sign in screen. Now create an onActivityResult() intent.
Then create onstart() method.
Thats it. You have successfully added firebase login to your app.
[post_ads]
How to add firebase authentication in android app
[implementation 'com.google.firebase:firebase-core:17.0.1' implementation 'com.firebaseui:firebase-ui-auth:5.0.0' implementation 'com.google.android.gms:play-services-auth:17.0.0' implementation 'com.facebook.android:facebook-android-sdk:5.2.0' ]
Now Sync your Gradle. Add facebook dependency only if you want to add facebook login to your app. Now import this statements in your main.activity.
[import com.firebase.ui.auth.AuthUI; import com.firebase.ui.auth.IdpResponse; import com.google.firebase.FirebaseApp; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; ]
Now go to Firebase Console and make a new project. Enter the name of your project, tick all the boxes and click on Accept.
Click on Android Icon to add your android app to your project.
Enter the details of your android app by entering details like app name, package name and SHA1 signature.
You can find a SHA1 signature in signing the report of your app.
[post_ads_2]
Once you click on register you will be asked to download google-services.json. Paste it in the root directory of your app.
Initialize this variable in your main activity class.
[private static final int RC_SIGN_IN = 1;]
In your onCreate() method initialize firebase.
[FirebaseApp.initializeApp(this);]
In your Main Activity class, create a signInIntent.
[public void createSignInIntent() {
List<AuthUI.IdpConfig> providers = Arrays.asList( new AuthUI.IdpConfig.GoogleBuilder().build(), new AuthUI.IdpConfig.FacebookBuilder().build()); startActivityForResult( AuthUI.getInstance() .createSignInIntentBuilder() .setAvailableProviders(providers) .setTosAndPrivacyPolicyUrls( "YOUR TOS URL", "YOUR PRIVACY POLICY URL") .setLogo(R.drawable.my_great_logo) .setTheme(R.style.Mytheme) .build(), RC_SIGN_IN);}]
In .setlogo you can set your logo for showing logo on your sign in screen. Now create an onActivityResult() intent.
[@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(); Toast.makeText(this, user.getDisplayName(), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Sign in error", Toast.LENGTH_SHORT).show(); } }}]
Then create onstart() method.
[@Override
protected void onStart() { FirebaseApp.initializeApp(MainActivity.this); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user==null){ createSignInIntent(); } else { //Already signed in Toast.makeText(this, user.getDisplayName(), Toast.LENGTH_SHORT).show(); } super.onStart();}]
Thats it. You have successfully added firebase login to your app.
COMMENTS