Ad

FirebaseAuth And FirebaseDatabase Not Saving Google Sign Ups

I have a system that saves createUserWithEmailAndPassword data to a realtime database and to the authentication database. But after making a similar system using google sign in instead nothing will save to the database and neither does anything save to the Authentication database.

ive tried using Log.e, ive tried debugging the app and also tried to decode the code...

heres some code:

package com.brandshopping.brandshopping;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.HashMap;

public class LoginOrSignupActivity extends AppCompatActivity {

    private Button LoginBtn, RegisterWithEmailBtn, RegisterWithGoogleBtn;
    private String Tag;
    private ProgressDialog LoadingBar;

    private FirebaseDatabase firebasedatabase = FirebaseDatabase.getInstance();
    private DatabaseReference database = firebasedatabase.getReference();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_or_signup);

        LoadinGUI();



        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

        GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso); //Create Google sign in object


        LoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginOrSignupActivity.this, LogInActivity.class));
            }
        });

        RegisterWithEmailBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginOrSignupActivity.this, RegisterActivity.class));
            }
        });

        RegisterWithGoogleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (v.getId()) {
                    case R.id.Register_WithGoogle_btn:
                        signIn();
                        break;
                }

           }
        });
    }

    private void signIn() {

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

        GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(LoginOrSignupActivity.this, gso); //Create Google sign in object

        Intent signInIntent = mGoogleSignInClient.getSignInIntent();

        int RC_SIGN_IN = 100;

        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        int RC_SIGN_IN = 100;

        // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            // The Task returned from this call is always completed, no need to attach
            // a listener.
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }

    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);
            Toast.makeText(this, "Register/signin successful", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(LoginOrSignupActivity.this, AccountInfoActivity.class));




        } catch (ApiException e) {
            // The ApiException status code indicates the detailed failure reason.
            // Please refer to the GoogleSignInStatusCodes class reference for more information.
            Toast.makeText(this, "Log in failed", Toast.LENGTH_SHORT).show();
            Log.e(Tag, "error: ");
            startActivity(new Intent(LoginOrSignupActivity.this, AccountInfoActivity.class));

        }
    }

    void SaveToDataBase(){

        database.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                GoogleSignInAccount Guser = GoogleSignIn.getLastSignedInAccount(LoginOrSignupActivity.this);
                String EmailWithEtheraRemoved = Guser.getEmail().replace(".", " ");

                if(!(dataSnapshot.child("Users").child(EmailWithEtheraRemoved).exists())){
                    LoadingBar.setMessage("Please wait while we load the credentialls in");
                    LoadingBar.setTitle("Register");
                    LoadingBar.setCanceledOnTouchOutside(false);
                    LoadingBar.show();

                    HashMap<String, Object> Userdatamap = new HashMap<>();

                    Userdatamap
                            .put("Email", Guser.getEmail());

                    Userdatamap
                            .put("Phone number", "Google intigrated sign in does not allow phone number requesting... This will be fixed in later patches");

                    Userdatamap
                            .put("Name", Guser.getGivenName() + Guser.getFamilyName());

                    if(Guser != null){
                        Userdatamap
                                .put("Created with", "Intigrated Google sign in");
                    }

                    database
                            .child("Users")
                            .child(EmailWithEtheraRemoved)
                            .updateChildren(Userdatamap)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    LoadingBar.dismiss();
                                    Toast.makeText(LoginOrSignupActivity.this, "Database save successful", Toast.LENGTH_SHORT).show();
                                    Log.e("SignUpError :", task
                                            .getException()
                                            .getMessage());


                                }
                            }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(LoginOrSignupActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
                            Log.e(Tag, "error: ");
                        }
                    });

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }

    void LoadinGUI(){

        LoginBtn = (Button) findViewById(R. id. Login_btn);
        RegisterWithEmailBtn = (Button) findViewById(R. id. Register_WithEmail_btn);
        RegisterWithGoogleBtn = (Button) findViewById(R. id. Register_WithGoogle_btn);

    }

}

I am expecting the app to save info to the realtime database as well as the authentication database. Niether of that seems to be working...

Ad

Answer

You forgot to call the SaveToDataBase after sign in Successfully. That's why there's no log and database entry.

Ad
source: stackoverflow.com
Ad