我搜索了很多网站,但没有找到使用 Firebase 在 Flutter 中实现电话身份验证的方法。谁能告诉我怎么做?
最佳答案
有据可查的工作演示项目here
下面是详细过程
AuthCrendential
对象AuthCredential
对象是唯一用于登录用户的东西。
它可以从 verifyPhoneNumber
中的 verificationCompleted
回调函数或 PhoneAuthProvider
中获得。(不要担心,如果它令人困惑,继续阅读,你会明白的)
phoneNumber
phoneNumber
的 SIM 卡不在当前运行该应用的设备中,
AuthCredential
对象AuthCredential
来登录
即使 phoneNumber
在设备中,此方法也有效submitPhoneNumber
函数的 verificationCompleted
回调提供了登录用户所需的 AuthCredential
对象Future<void> _submitPhoneNumber() async {
/// NOTE: Either append your phone number country code or add in the code itself
/// Since I'm in India we use "+91 " as prefix `phoneNumber`
String phoneNumber = "+91 " + _phoneNumberController.text.toString().trim();
print(phoneNumber);
/// The below functions are the callbacks, separated so as to make code more readable
void verificationCompleted(AuthCredential phoneAuthCredential) {
print('verificationCompleted');
...
this._phoneAuthCredential = phoneAuthCredential;
print(phoneAuthCredential);
}
void verificationFailed(AuthException error) {
...
print(error);
}
void codeSent(String verificationId, [int code]) {
...
print('codeSent');
}
void codeAutoRetrievalTimeout(String verificationId) {
...
print('codeAutoRetrievalTimeout');
}
await FirebaseAuth.instance.verifyPhoneNumber(
/// Make sure to prefix with your country code
phoneNumber: phoneNumber,
/// `seconds` didn't work. The underlying implementation code only reads in `milliseconds`
timeout: Duration(milliseconds: 10000),
/// If the SIM (with phoneNumber) is in the current device this function is called.
/// This function gives `AuthCredential`. Moreover `login` function can be called from this callback
verificationCompleted: verificationCompleted,
/// Called when the verification is failed
verificationFailed: verificationFailed,
/// This is called after the OTP is sent. Gives a `verificationId` and `code`
codeSent: codeSent,
/// After automatic code retrival `tmeout` this function is called
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout,
); // All the callbacks are above
}
void _submitOTP() {
/// get the `smsCode` from the user
String smsCode = _otpController.text.toString().trim();
/// when used different phoneNumber other than the current (running) device
/// we need to use OTP to get `phoneAuthCredential` which is inturn used to signIn/login
this._phoneAuthCredential = PhoneAuthProvider.getCredential(
verificationId: this._verificationId, smsCode: smsCode);
_login();
}
Future<void> _login() async {
/// This method is used to login the user
/// `AuthCredential`(`_phoneAuthCredential`) is needed for the signIn method
/// After the signIn method from `AuthResult` we can get `FirebaserUser`(`_firebaseUser`)
try {
await FirebaseAuth.instance
.signInWithCredential(this._phoneAuthCredential)
.then((AuthResult authRes) {
_firebaseUser = authRes.user;
print(_firebaseUser.toString());
});
...
} catch (e) {
...
print(e.toString());
}
}
Future<void> _logout() async {
/// Method to Logout the `FirebaseUser` (`_firebaseUser`)
try {
// signout code
await FirebaseAuth.instance.signOut();
_firebaseUser = null;
...
} catch (e) {
...
print(e.toString());
}
}
更多实现细节请引用lib/main.dart
文件here .
如果您发现问题,欢迎对此答案和 repo README 进行编辑。
https://stackoverflow.com/questions/50181000/
相关文章:
android - 如何在 Flutter 中禁用发布版本中的所有日志 [debugPrint()]
javascript - 在 Flutter Webview 中运行一些 Javascript 文件
flutter - 在 ListView 中的键盘上方滚动 TextFormField
flutter - 需要不同屏幕纵横比和纵向和横向模式的聊天屏幕背景图片
android-studio - Flutter - 尝试运行 Flutter 应用程序时模拟器未启
android-studio - Flutter/Dart 自动完成功能适用于 VS Code,但不