You can do your users authentication with Firebase using their Google Accounts by integrating Google Sign-In. When user signs in for the first time, a new user account is created This new account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of how the user signs in.
- In your apps, you can get the user’s basic profile information from the FirebaseUser object.
- In your Firebase Realtime Database and Cloud Storage you can get the signed-in user’s unique user ID and use this id to control data of user.
For this, you have to add firebase in your project. Following are the steps for gmail login using firebase.
- Add the following dependencies for Firebase Authentication and Google Sign-In to your app build.gradle file :
- compile ‘com.google.firebase:firebase-auth:11.4.2’
compile ‘com.google.android.gms:play-services-auth:11.4.2’ - Specify app’s SHA-1 fingerprint on firebase console from setting.
- Enable Google Sign-In in the Firebase console :
- Open “Auth” section in firebase
- On the “Sign in method ” tab, enable the Google sign-in method and save.
Authentication use following override methods:
1. Configure the GoogleSignInOptions object and call requestIdToken.
// Google Sign In configuration
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
Pass server_client_id to the requestIdToken method. OAuth 2.0 client ID can be find in this way:
- Open the https://console.developers.google.com/projectselector/apis/credentials?pli=1&supportedpurview=project in the API Console.
- The web application type client ID is your backend server’s OAuth 2.0 client ID.
After integration of Google Sign-In,Following is the code of your sign in Activity:
private void signIn() { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } @Override 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); } else { // Google Sign In failed, update UI appropriately // ... } } } In your sign-in activity's onCreate method, get the shared instance of the FirebaseAuth object:
- After a user successfully signs in, get an ID token from the
GoogleSignInAccount
object, exchange it for a Firebase credential, and authenticate with Firebase using the Firebase credential:private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId()); AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCredential:success"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCredential:failure", task.getException()); Toast.makeText(GoogleSignInActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); updateUI(null); } // ... } }); }
To Sign Out , User call Sign Out method
FirebaseAuth.getInstance().signOut();