使用Firebase进行手机号注册和登录

huangapple go评论81阅读模式
英文:

Sign up & Login with Phone Number in Firebase

问题

这是我的 OTP 页面:

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
    firebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()){
                        Intent intent = new Intent(B_Verification_Activity.this, C_Name_Activity.class);
                        intent.putExtra("userPhone", comp_phoneNumber);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                    }
                    else{
                        return;
                    }
                }
            });
}

这是我在最后一个活动中将数据存储到 Firebase 中的代码:

public void storeNewUserData() {
    rootNode = FirebaseDatabase.getInstance();
    String userId = firebaseAuth.getCurrentUser().getUid();
    reference = rootNode.getInstance().getReference().child("Kullanıcılar").child(user_Gender.toString()).child(userId);
    
    UserHelperClass addNewUser = new UserHelperClass(user_phoneNumber, user_FirstName, user_Gender, user_Birthdate, user_Hobbies);
    reference.setValue(addNewUser);
}

但我不知道如何使用电话号码让用户登录。在注册页面,我只是验证了电话号码,就这样而已。我不知道用户是否已在注册活动中被创建。我想在 OTP 活动中创建用户。我是否需要编写其他代码?

我的英语不是很好,谢谢您的帮助。

英文:

I am new in Android and I have few questions about when a new user account is created on phone authentication. I created a Register Activity and OTP Activity for user registration. After the user registration, I get the user's information and 5 Activity later I store the datas to Firebase. I can see the user's details and phone number in Firebase.

This is my OTP page:

  private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {

    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

    firebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
                @Override
                public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                    if(task.isSuccessful()){
                        
                        Intent intent = new Intent(B_Verification_Activity.this, C_Name_Activity.class);
                        intent.putExtra(&quot;userPhone&quot;, comp_phoneNumber);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                    }
                    else{
                        return;
                    }
                }
            });
}

And this is my last activity that I store the datas in Firebase:

  public void storeNewUserData() {
    rootNode = FirebaseDatabase.getInstance();
    String userId = firebaseAuth.getCurrentUser().getUid();
    reference = rootNode.getInstance().getReference().child(&quot;Kullanıcılar&quot;).child(user_Gender.toString()).child(userId);

    UserHelperClass addNewUser = new UserHelperClass(user_phoneNumber, user_FirstName, user_Gender, user_Birthdate, user_Hobbies);
    reference.setValue(addNewUser);
}

But I don't know how user to login with phone number. In registration page, I just verified phone number, that's it. I don't know user is created or not in Register Activity. I want to create user in OTP activity. Is there any other code to I should write?

My english is not very good, thank you for helping.

答案1

得分: 4

在 Firebase 手机验证中,没有注册或登录功能。实际上,他们只会获取用户的手机号码,并使用一次性密码对其进行验证。这实际上是手机号码验证。
换句话说,如果手机号是新的,那么在验证后将创建一个新的用户帐户。但是,如果返回的用户验证了他/她的号码,他们只是被登录了。你可能想从这里做一件事:
你可能想检查用户是新用户还是老用户,然后从实时数据库/ Firestore 获取数据(如果他们是返回的用户)。如果是新用户,你可能还会将数据发送到数据库,如果是这种情况,你可以在用户第一次使用时发送他们的数据。
为了帮助你,Firebase Android SDK 中有一个名为 isNewUser() 的函数,如果他们是第一次使用其号码进行验证(登录),则返回布尔值 true。你可以从这里详细了解该函数。

英文:

There is no registration or login functionality in Firebase Phone Authentication. Actually, they just take user's phone number and verify it with a one time password. It is phone number verification actually.
In other terms, if the phone number is new, then a new user account is created after verification. But if a returning user verifies his/her number, they are simply signed in. You might want to do one thing from here :<br>
You may want to check if user is new or old and then get data from Realtime database/Firestore if they are returning users. You might be also sending data of new user to database, in that case you can send their data if they are first time user.
To help you with this, there is a function in Firebase Android SDK named isNewUser() which returns boolean value true if they are verifying (logging) with their number for first time. You may read about that function in detail from here.

huangapple
  • 本文由 发表于 2020年8月30日 12:34:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63653985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定