0
votes

I have a query regarding Firebase Authentication.

I have used Firebase Auth in my Android Studio project.

When someone uses google sign in method provided in my app a new account is created and then when someone signs in using email sign in method using the same email address as google account, no new account is created instead the same account is added to the previous user ID.

I have added firestore to the project where I keep data about users.

Recently I added phone authentication and when the user logs in, if the phone no exists in firestore database I dont want a new account with new UID to be created. Instead I want the account to merge with the email accounts.

Please help me to achieve this.

1

1 Answers

0
votes

Define that

private  FirebaseFirestoreSettings settings ;

in a Button of signUp

settings = new FirebaseFirestoreSettings.Builder().setTimestampsInSnapshotsEnabled(true)
                                            .build();
                                    firebaseFirestore.setFirestoreSettings(settings);

                                    firebaseFirestore.collection("USERS")
                                            .add(userData)

here an example

        firebaseAuth.createUserWithEmailAndPassword(email.getText().toString() , password.getText().toString())
                                .addOnCompleteListener(new OnCompleteListener<AuthResult>()
                                {
                                    @Override
                                    public void onComplete(@NonNull Task<AuthResult> task)
                                    {
                                        if(task.isSuccessful())
                                        {
                                            Log.i(TAG , "checkEmailAndPassword : firebaseAuth.createUserWithEmailAndPassword  IS SUCCESSFUL");
                                            Map<String , Object> userData = new HashMap<>();
                                            userData.put("fullname" , fullName.getText().toString());
                                            String userid = firebaseAuth.getCurrentUser().getUid();
                                            Log.i(TAG , "HashMap Created successfully " + fullName.getText().toString());
                                            settings = new FirebaseFirestoreSettings.Builder().setTimestampsInSnapshotsEnabled(true)
                                                    .build();
                                            firebaseFirestore.setFirestoreSettings(settings);
        
                                            firebaseFirestore.collection("USERS")
                                                    .add(userData)
                                                    .addOnSuccessListener(new OnSuccessListener<DocumentReference>()
                                                    {
                                                        @Override
                                                        public void onSuccess(DocumentReference documentReference)
                                                        {
        
                                                                Log.i(TAG , "checkEmailAndPassword : firebaseFirestore.collection add(userData) IS SUCCESSFUL");
                                                                Log.i(TAG , "Account Created" + email.getText().toString());
                                                                mainIntent();
        
                                                        }
                                                    }).addOnFailureListener(new OnFailureListener()
                                            {
                                                @Override
                                                public void onFailure(@NonNull Exception e)
                                                {
        
                                                   
        
                                                    Log.i(TAG , "checkEmailAndPassword : firebaseAuth.create User With Email And Password ERROR");
                                                    String error = e.getMessage();
                                                    Log.e(TAG , "ERROR " + error);
        
                                                    Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
        
                                                }
                                            });
                      
    
                  
    
    }
else
                                {
                                    

                                    Log.i(TAG , "checkEmailAndPassword : firebaseAuth.create User With Email And Password ERROR");
                                    String error = task.getException().getMessage();
                                    Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();

                                }
                            }
                        });