Updated 17 August 2017
In this Blog, we are going to have a look at how to use Firebase Auth to Authenticate Users for Your Application.
Lets First have a look at the advantages of using Firebase Authentication:
You can have a detailed explanation for each point from the official documents.
Over here, I have used the default Firebase Auth UI. You can definitely surpass this and use your views.
APPROACH :
Let’s have a look at the code:
1 2 3 4 5 |
compile 'com.google.firebase:firebase-auth:11.0.1' compile 'com.firebaseui:firebase-ui-auth:1.2.0' <!-- to be added outside the dependenies block --> apply plugin: 'com.google.gms.google-services' |
1 |
classpath 'com.google.gms:google-services:3.0.0' |
1 2 |
// Request code used for starting activity for result private static final int RC_SIGN_IN = 1008; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
signInButton = (Button)findViewById(R.id.my_sign_in_button); signInButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivityForResult(AuthUI .getInstance().createSignInIntentBuilder() .setIsSmartLockEnabled(false) // to stop the default google-account-chooser pop up .setProviders(Arrays.asList( new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(), new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build(), new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(), new AuthUI.IdpConfig.Builder(AuthUI.TWITTER_PROVIDER).build() )).build(),RC_SIGN_IN); } }); |
1 2 3 4 5 6 7 8 9 10 11 12 |
@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){ //Replace with your own logic. Log.d("TAG", "FireBaseAuthActivity onActivityResult: " +response.getEmail()); } } } |
That’s all.
Also, don’t forget to add google-services.json file and enable Authentication methods you want to use from the firebase console.
NOTE: In order to enable the Authentication methods, all you need to do is:
And All is set.
This will enable all the SignIn methods you want to include in your Application.
Keep Coding and Keep Sharing 🙂
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.