简体   繁体   English

Firebase 关于电话认证的认证问题

[英]Firebase Auth problem regarding phone authentication

I have a query regarding Firebase Authentication.我对 Firebase 身份验证有疑问。

I have used Firebase Auth in my Android Studio project.我在我的 Android Studio 项目中使用了 Firebase Auth。

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.当有人使用我的应用程序中提供的谷歌登录方法时,会创建一个新帐户,然后当有人使用 email 登录方法时,使用与谷歌帐户相同的 email 地址登录方法,不会创建新帐户,而是将相同的帐户添加到以前的帐户中用户身份。

I have added firestore to the project where I keep data about users.我已将 firestore 添加到我保存用户数据的项目中。

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.最近我添加了电话身份验证,当用户登录时,如果 Firestore 数据库中不存在电话,我不希望创建具有新 UID 的新帐户。 Instead I want the account to merge with the email accounts.相反,我希望该帐户与 email 帐户合并。

Please help me to achieve this.请帮助我实现这一目标。

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();

                                }
                            }
                        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM