How to Implement Google Login using Firebase Auth using SHA certificate fingerprint and store user’s details in Firebase Realtime database?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our W3Make Forum to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
To implement Google Login using Firebase Auth with a SHA certificate fingerprint and store user details in Firebase Realtime Database, you can follow these steps:
Step 1: Set up Firebase Project
1. Go to the Firebase Console (https://console.firebase.google.com/) and create a new project.
2. Add an Android app to your project, providing the necessary details such as package name and SHA-1 certificate fingerprint. To obtain the SHA-1 fingerprint, you can use the following command in the terminal:
—————————————————————————————
keytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore
—————————————————————————————
Make sure to replace `~/.android/debug.keystore` with the path to your debug keystore. If you’re using a release keystore, provide the appropriate path and keystore details.
3. Download the `google-services.json` file for your app and place it in the appropriate module-level directory of your Android project.
Step 2: Configure Firebase Authentication
1. In the Firebase Console, navigate to your project’s Authentication section.
2. Enable Google as a sign-in provider and configure the necessary details, such as Web client ID and package name.
Step 3: Set up Android Project
1. In your Android project, add the Firebase SDK dependencies to your app-level ——–build.gradle file:———————————————————————
gradle
implementation ‘com.google.firebase:firebase-auth:21.0.1’
implementation ‘com.google.firebase:firebase-database:21.0.1’
implementation ‘com.google.android.gms:play-services-auth:19.2.0’
————————————————————————————————–
2. Sync your project to fetch the new dependencies.
Step 4: Implement Google Login
1. In your Android app’s login activity, add a Google Sign-In button to your layout XML:
————————————————————————————————xml
<com.google.android.gms.common.SignInButton
android:id=”@+id/googleSignInButton”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
———————————————————————————————–
2. In your login activity’s Java file, initialize Firebase Auth and GoogleSignInOptions in the `onCreate` method:
———————————————————————————————-java
private FirebaseAuth mAuth;
private GoogleSignInClient mGoogleSignInClient;
private static final int RC_SIGN_IN = 123; // Request code for the sign-in intent
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
// Configure Google Sign-In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
// Set click listener for the sign-in button
SignInButton signInButton = findViewById(R.id.googleSignInButton);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
}
———————————————————————————————-
3. Implement the `signIn` method to start the Google Sign-In flow:
———————————————————————————————-java
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
“`
4. Handle the sign-in result in the `onActivityResult` method:
———————————————————————————————-java
@Override
protected 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) {