通过电话号码解决方法登录Firebase

时间:2018-03-19 13:47:05

标签: android firebase firebase-authentication

所以我想到了firebase中的一种解决方法(实际上非​​常简单)通过电话号码而不是电子邮件登录。这是当登录电话号码与电子邮件相关联并且收听者获得该号码下的电子邮件时,该电子邮件将通过SignInWithEmaiLAndPassword登录,但是,它似乎没有进入最后一道屏障。也就是说,我首先会显示我的代码。这是我的代码段:

获取值和SignInWithEmail部分的侦听器

//ONCLICK LISTENER
loginBtn.setOnClickListener(new View.OnClickListener() {
    @Override
                                public void onClick(View view) {
        //Perform Login Operation
        userEmailString = userEmailEdit.getText().toString().trim();
        userPasswordString = userPasswordEdit.getText().toString().trim();

        if (!TextUtils.isEmpty(userEmailString) && !TextUtils.isEmpty(userPasswordString) && userPasswordString != null && userEmailString != null) {
            /*UPDATE*/

            final FirebaseDatabase database = FirebaseDatabase.getInstance();
            String location = "Users/" + userEmailString;
            Toast.makeText(LoginActivity.this, location, Toast.LENGTH_LONG).show();

            DatabaseReference ref = database.getReference(location);
            ref.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot1) {
                    if (dataSnapshot1 != null) {
                        Toast.makeText(LoginActivity.this, "NOT NULL", Toast.LENGTH_LONG).show();

                        //dataSnapshot points to the User node with list of users
                        //Retrieve data from the childrens emailUser,passWordUser,userID and Invoke start activity
                        for (DataSnapshot childDataSnapshot : dataSnapshot1.getChildren()) {

                            final String userEmail = String.valueOf(dataSnapshot1.child("USER_EMAIL").getValue());
                            Toast.makeText(LoginActivity.this, userEmail, Toast.LENGTH_LONG).show();

                            mAuth.signInWithEmailAndPassword(userEmail, userPasswordString).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    if (task.isSuccessful())
                                    {
                                        Toast.makeText(LoginActivity.this, "Task Succesful", Toast.LENGTH_LONG).show();
                                        mDatabaseRef.addValueEventListener(new ValueEventListener() {
                                            @Override
                                            public void onDataChange(DataSnapshot dataSnapshot) {
                                                checkUserValidation(dataSnapshot, userEmail);
                                            }

                                            @Override
                                            public void onCancelled(DatabaseError databaseError) {
                                            }
                                        });
                                    }else
                                    {
                                        Toast.makeText(LoginActivity.this, "User Login Failed", Toast.LENGTH_LONG).show();
                                        Toast.makeText(LoginActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
                                    }
                                }
                            });
                                /*Added*/
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
            /*/UPDATE*/
        }
    }
});

验证部分

private void checkUserValidation(DataSnapshot dataSnapshot, String emailForVer) {
    Iterator iterator  = dataSnapshot.getChildren().iterator();

    Toast.makeText(LoginActivity.this, "Made it to validation", Toast.LENGTH_LONG).show();    

    while(iterator.hasNext())
    {
        DataSnapshot dataUser  = (DataSnapshot) iterator.next();
        Toast.makeText(LoginActivity.this, "Now on Snapshot", Toast.LENGTH_LONG).show();

         if(String.valueOf(dataUser.child("emailUser").getValue()).equals(emailForVer) && String.valueOf(dataUser.child("userPhone").getValue()).equals(emailForVer) && dataUser.child("emailUser") != null && dataUser.child("userName") !=null) {
             Intent accountActivity = new Intent(LoginActivity.this, Welcome.class);
                 Toast.makeText(LoginActivity.this, "Made it to final", Toast.LENGTH_LONG).show();

                 String user = String.valueOf(dataUser.child("userName"));
                accountActivity.putExtra("USER_NAME", user);
                startActivity(accountActivity);
                finish();
        }
    }
}

最后一个障碍是我想展示的最后一个Toast,它的谚语&#34;使它最终成为&#34;我可以对你为何无法解决这个问题有所了解吗?非常感谢你。

也不建议我使用firebase的手机验证,我已经知道了,所以这真的只是一种解决方法,因为我没有时间了。(最后一分钟改变让你哭)好吧,我的Firebase结构很酷,我能够获取我想要的东西。除了我不能通过最后的祝酒词。请帮忙。

0 个答案:

没有答案