简体   繁体   English

Android:如何使用他的电话号码登录用户

[英]Android: How to login an user with his phone number

I'm creating an app with where I want already signed up users to be redirected to their profile if their phone number is registered in the app, but if he is a new user then he will be redirected to the welcome page.我正在创建一个应用程序,如果他们的电话号码已在应用程序中注册,我希望已经注册的用户被重定向到他们的个人资料,但如果他是新用户,那么他将被重定向到欢迎页面。 The problem is, I'm able to get the verification code for the phone number, but it is always redirecting to the welcome page irrespective of whether it is an existing user or a new user.问题是,我能够获取电话号码的验证码,但无论是现有用户还是新用户,它总是重定向到欢迎页面。 By far I've implemented Firebase phone authentication, and到目前为止,我已经实现了 Firebase 电话身份验证,并且

    private void verifyCode(String code) {

        PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, code);
        signInWithCredentials(phoneAuthCredential);

    }

    private void signInWithCredentials(PhoneAuthCredential phoneAuthCredential) {

        mAuth.signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {


                final String num = getIntent().getStringExtra("phoneNumber");
                final Query query = FirebaseDatabase.getInstance().getReference("users").orderByChild("birthday").equalTo(num);

                if (task.isSuccessful()) {

                    if (num.equals(query.toString())) {

                        Log.i("Method", "Inside if block");
                        Log.i("value", num + query.toString());
                        Intent intent = new Intent(VerifyPhoneActivity.this, WelcomeActivity.class);

                        startActivity(intent);

                    } else {

                        Log.i("Method", "Inside if block2");
                        Log.i("value", num + query.toString());



                    Intent intent = new Intent(VerifyPhoneActivity.this, ProfileActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

                    startActivity(intent);
                }


                } else {

                    Toast.makeText(VerifyPhoneActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();

                }
            }
        });

    }

I'm taking only the phone number of the user to log in/sign up, and there's no separate button for login and sign up.我只使用用户的电话号码登录/注册,并且没有单独的登录和注册按钮。 It should work in such a way that, once the OTP sent by the Firebase auth has been verified and if the phone number is already present in the database, it should directly fo the user's profile page instead of the welcome screen, however, if the phone number is new it should go to the welcome page instead of the user profile page.它应该以这样的方式工作,一旦 Firebase auth 发送的 OTP 已经过验证并且如果电话号码已经存在于数据库中,它应该直接出现在用户的个人资料页面而不是欢迎屏幕上,但是,如果电话号码是新的,它应该转到欢迎页面而不是用户个人资料页面。 The challenge I'm facing is I'm not able to check if the number entered is already present in the database in the user table(I've created a separate user table to store the details of the user when he signs up for the first time).我面临的挑战是我无法检查输入的数字是否已经存在于用户表的数据库中(我创建了一个单独的用户表来存储用户在注册时的详细信息)第一次)。

from what I understood you're able to register users but you're not able to redirect unregistered users to the login/signup screen, if that's the case then you can try to use据我了解,您可以注册用户,但您无法将未注册的用户重定向到登录/注册屏幕,如果是这种情况,那么您可以尝试使用

FirebaseAuth.getInstance().currentUser

in the on create view method in your welcome/home page it should be something like this在您的欢迎/主页的 on create view 方法中,它应该是这样的

override fun onCreate(savedInstanceState: Bundle?) {

  if (FirebaseAuth.getInstance().currentUser == null) {
     startActivity(Intent(applicationContext, RegistrationActivity::class.java))
     finish()
     return
  } 

  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_main)
  // do your other stuff

}

this code is written in kotlin it shouldn't be that different in java.这段代码是用 kotlin 编写的,在 java 中应该没有什么不同。 hope it helps.希望能帮助到你。

UPDATE------更新 - - -

you have a few mistakes with your code, try this你的代码有一些错误,试试这个

private void verifyCode(String code) {

    PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, code);

    FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {

                final String num = getIntent().getStringExtra("phoneNumber");
                FirebaseDatabase.getInstance().getReference("users").orderByChild("phoneNumber").equalTo(num).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {
                            Intent intent = new Intent(VerifyPhoneActivity.this, ProfileActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        } else {
                            FirebaseDatabase.getInstance().getReference("users").push().child("phoneNumber").setValue(num).addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    Intent intent = new Intent(VerifyPhoneActivity.this, WelcomeActivity.class);

                                    startActivity(intent);

                                }
                            });
                        }
                    }

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

                    }
                });
            } else {
                Toast.makeText(VerifyPhoneActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    });
}

first of all, you have to log in the user with the phone detail then you check if you have any user registered with that phone number if you do then that means that this user has already entered the app but if you didn't have any information of user with that phone number then you create his information and direct him to the welcome screen after creating his information, so if this user comes back again you will check if we have a record of user with that phone number (which we do) so this time he will be directed to the profile screen首先,您必须使用电话详细信息登录用户,然后检查是否有任何用户使用该电话号码注册,如果这样做,则意味着该用户已经进入该应用程序,但如果您没有具有该电话号码的用户的信息然后您创建他的信息并在创建他的信息后将他定向到欢迎屏幕,因此如果该用户再次回来,您将检查我们是否有该电话号码的用户记录(我们这样做)所以这次他将被引导到个人资料屏幕

暂无
暂无

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

相关问题 如何从他的登录信息打印用户类型? - how to print the user type from his login? Android:检测用户是否在一段时间内停止使用手机 - Android: Detect if user stopped using his phone for a while 更改登录名时如何更新 Principal 中的用户数据? - How to update user data in Principal when changing his login? 如何包含另一个登录页面,并根据用户的URL将用户定向到两个登录页面之一? - How do I include another login page and direct the user to one of the two login pages based on his url? 如何使用 firebase android studio 的电话号码创建登录和注册? - How can I create login and sign up using phone number with firebase android studio? 使用现有用户登录应用程序 - 手机 Auth Firebase Android - - Login app with existing user - phone Auth Firebase Android - 当用户登录并想用新密码更改旧密码时如何验证用户 - How to validate user when he login and wants to change his old password with new password 使用apache shiro登录后如何将用户重定向到他的个人页面 - how to redirect user to his personal page after login using apache shiro 如何使用 android 工作室在 firebase 中的用户输入电话号码的帮助下更新子节点(密码)? - How to update child node (password) with the help of user entered phone number in firebase using android studio? 如何在不使用 email 或 firebase android 中的电话号码的情况下制作自定义用户帐户 - How to make a custom user account without using email or phone number in firebase android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM