- 1. Create a new android project
- 2. Set up Firebase Authentication for android a. Using Firebase Assistant
b. Manually Add Firebase1. Add rules to the root-level build.gradle to include the google-services plugin
(classpath ‘com.google.gms:google-services:3.0.0’)
2. Enable the google-services gradle plugin in the app-level build.gradle
(apply plugin: ‘com.google.gms.google-services’)
3. Add the dependencies for firebase SDKs in the app-level build.gradle file
(com.google.firebase:firebase-auth:10.2.0)
4. Add play services authentication library
(compile ‘com.google.android.gms:play-services-auth:10.0.1’) - 3. Check if the user is already logged in
- -Create instances of FirebaseAuth and FirebaseAuth.AuthStateListener (e.g. mAuth and mAuthStateListener repectively)private FirebaseAuth mAuth;
private FirebaseAUthStateListener;->Initialize these inside onCreate()/*get firebase auth instance*/ mAuth=FirebaseAuth.getInstance(); mAuthStateListener=new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user=firebaseAuth.getCurrentUser(); if(user!=null) { /*User is logged in*/ } else { /*User not logged in*/ } } };
- -Add the mAuthStateListener to the mAuth object inside onStart()mAuth.addAuthStateListener(mAuthStateListener) ;
- -Remove the auth state listener inside onStop()
if(mAuthStateListener!=null) mAuth.removeAuthStateListener(mAuthListener);
- -Create instances of FirebaseAuth and FirebaseAuth.AuthStateListener (e.g. mAuth and mAuthStateListener repectively)
- 4. Create a GoogleSignInOptions object and use it to build a google api client object
We need this object to configure google sign-in to request required user data
// Configure Google Sign In (inside onCreate) GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestEmail() .build(); mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build();
The parameter GoogleSignInOptions.DEFAULT_SIGN_IN gives user’s ID and basic profile
- 5. Add the Google Login Button to your activity’s layout
<com.google.android.gms.common.SignInButton android:id="@+id/sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" />
- 6. On clicking this button launch the sign in intent from google api and handle the result in onActivityResult()
SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button); signInButton.setOnCLickListener(new View.OnClickListener(){ @override public void onClick(View view) { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } })
Handle the result from this Intent in onActivityResult.
-Get the GoogleSignInResult object and check if Google sign in is successful ,
-If yes get GoogleSignInAccount instance , we shall pass this account instance to the method firebaseAuthWithGoogle(GoogleSignInAccount account); which we are going to implement in the next step.public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); if (result.isSuccess()) { // Google Sign In was successful, authenticate with Firebase GoogleSignInAccount account = result.getSignInAccount(); firebaseAuthWithGoogle(account); //We shall implement this in next steps } else { // Google Sign In failed, update UI appropriately // ... } } }
- 7. Implementation of firebaseAuthWithGoogle(GoogleSignInAccount account)-Get the ID token from GoogleSignInAccount object
-Get the Firebase credentials by passing this id token to GoogleAuthProvider.getCredential()
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
-Now authenticate the user with Firebase using these credentialsmAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (!task.isSuccessful()) { //Failure Toast.makeText(GoogleSignInActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } else { //success navigate to LoggedIn Activity } } });
Firebase Authentication with Google Sign in
- 1. Create a new android project
- 2. Set up Firebase Authentication for android
- 3. Check if the user is already logged in
- -Create instances of FirebaseAuth and FirebaseAuth.AuthStateListener (e.g. mAuth and mAuthStateListener repectively)
- -Add the mAuthStateListener to the mAuth object inside onStart()
- -Remove the auth state listener inside onStop()
- -Create instances of FirebaseAuth and FirebaseAuth.AuthStateListener (e.g. mAuth and mAuthStateListener repectively)
- 4. Create a GoogleSignInOptions object and use it to build a google api client object
- 5. Add the Google Login Button to your activity’s layout
- 6. On clicking this button launch the sign in intent from google api and handle the result in onActivityResult()
- 7. Implementation of firebaseAuthWithGoogle(GoogleSignInAccount account)