[英]login with mobile number via otp through firebase authentication
In SignUpActivity.java I am taking the mobile number from user and passing it to another intent where the user needs to enter the otp or it gets fill up automatically.This is working fine through my code but I want to make that type of fragment thing by which if user submits the details in SignUpActivity.java then a fragment kind of thing appears as shown in the screenshot for detecting the otp.在 SignUpActivity.java 中,我从用户那里获取手机号码并将其传递给用户需要输入 otp 或自动填充的另一个意图。这通过我的代码工作正常,但我想制作那种类型的片段如果用户在 SignUpActivity.java 中提交详细信息,则会出现一个片段类型的东西,如用于检测 otp 的屏幕截图所示。 Please someone say that how to achieve this.
请有人说如何实现这一点。 image of the result which I want
我想要的结果的图像
You can see info of that here https://firebase.google.com/docs/auth/android/phone-auth你可以在这里看到信息https://firebase.google.com/docs/auth/android/phone-auth
// The test phone number and code should be whitelisted in the console.
String phoneNumber = "some_number";
String smsCode = "some_codes";
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseAuthSettings firebaseAuthSettings = firebaseAuth.getFirebaseAuthSettings();
// Configure faking the auto-retrieval with the whitelisted numbers.
firebaseAuthSettings.setAutoRetrievedSmsCodeForPhoneNumber(phoneNumber, smsCode);
PhoneAuthProvider phoneAuthProvider = PhoneAuthProvider.getInstance();
phoneAuthProvider.verifyPhoneNumber(
phoneNumber,
60L,
TimeUnit.SECONDS,
this, /* activity */
new OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
// Instant verification is applied and a credential is directly returned.
}
... /* other callbacks */
}
To add a fragment.添加一个片段。 First add a container in your layout file.
首先在布局文件中添加一个容器。
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<
...........
/>
// This is container for your fragment
<FrameLayout
android:id="@+id/YOU_CONTAINER_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now, create another layout file and java class for fragment.现在,为片段创建另一个布局文件和 java class。 Then write below java code in your activity
然后在您的活动中写下 java 代码
public static final int FRAGMENT_CODE = 1111;
.....
Bundle bundle = new Bundle();
bundle.putString("MobNo", YOUR_PHONE_NO_STRING);
MyFragment fragment = new MyFragment();
fragment.setArguments(bundle);
fragment.setTargetFragment(this,FRAGMENT_CODE);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.YOUR_CONTAINER_ID, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Now, to get phone number in your fragment现在,在您的片段中获取电话号码
if (getArguments() != null) {
String phoneNo = getArguments().getString("MobNo"));
}
AuthUI gives you that without any code from your side AuthUI 无需任何代码即可为您提供
do that implementation 'com.firebaseui:firebase-ui-auth:6.2.0'
add this line to build.gradle and update to latest version implementation 'com.firebaseui:firebase-ui-auth:6.2.0'
将此行添加到 build.gradle 并更新到最新版本
// Choose authentication providers
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.PhoneBuilder().build()); // can add more providers in future in that list without breaking existing code
// Create and launch sign-in intent
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),
REQUEST_CODE);
That is it here is the official documentation for that and don't forget to enable phone auth from the console就是这里的官方文档,不要忘记从控制台启用电话身份验证
also you mentioned in a comment that you don't know fragments so I will encourage you to learn the android basics first before moving to firebase你还在评论中提到你不知道片段,所以我鼓励你先学习 android 基础知识,然后再转到 firebase
EDIT编辑
Regarding collection of user info关于收集用户信息
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String name = user.getDisplayName();
String phone = user.getPhoneNumber();
String email = user.getEmail();
Uri photoUrl = user.getPhotoUrl();
note if you are authenticating via phone, only user.getPhoneNumer();
请注意,如果您通过电话进行身份验证,则只有
user.getPhoneNumer();
will return a phone number and other methods will return null, and in case of mail authentication user.getPhoneNumber()
will return null, in practice you should check all of them for nullity before using them somewhere.将返回一个电话号码,其他方法将返回 null,并且在邮件身份验证的情况下
user.getPhoneNumber()
将返回 null,实际上您应该在某处使用它们之前检查所有这些是否为空。
Have a good day Rohan祝你有个美好的一天罗汉
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.