Flutter Firebase Phone Authentication
image source https://www.youtube.com/watch?v=uaerc_uy-ME&ab_channel=SantosEnoque
Flutter supports varius ways to log in to firebase authentication.
One of them is the phone verification, which google sends OTP to the user's phone number.
This sign in function requires PhoneAuthCredential
FirebaseAuth _auth = FirebaseAuth.instance;
String verificationId = '';
bool showLoading = false;
void signInWithPhoneAuthCredential(
PhoneAuthCredential phoneAuthCredential) async {
setState(() {
showLoading = true;
});
try {
final authCredential =
await _auth.signInWithCredential(phoneAuthCredential);
setState(() {
showLoading = false;
});
if (authCredential.user != null) {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => LandingRoute()));
}
} on Exception catch (e) {
setState(() {
showLoading = false;
print('err from signInWithPhoneAUthCredential function call back');
currentState = MobileVerificationState.SHOW_MOBILE_FORM_STATE;
});
print(e);
}
}
PhoneAuthCredential is created viaawait _auth.verifyPhoneNumber(
phoneNumber: processedPhoneNumber,
verificationCompleted: (phoneAuthCredential) async {
setState(() {
print('verificationCompleted');
showLoading = false;
});
signInWithPhoneAuthCredential(
phoneAuthCredential); //not needed yet
},
verificationFailed: (verificationFailed) async {
setState(() {
print('verificationFailed');
showLoading = false;
});
print(verificationFailed.message);
setState(() {
showLoading = false;
currentState =
MobileVerificationState.SHOW_MOBILE_FORM_STATE;
});
print('error from verificationFailed');
},
codeSent: (verificationId, resendingToken) async {
print('codeSent');
setState(() {
showLoading = false;
currentState =
MobileVerificationState.SHOW_OPT_FORM_STATE;
this.verificationId = verificationId;
});
},
codeAutoRetrievalTimeout: (verificationId) async {},
);
}
ライターと火線ライターで携帯電話にログインすることができます.なお、Admin SDKを使用しないと、会員加入の有無を事前にチェックすることはできません.
このメソッドは、電子メールでログインしたときにauthになりました.内蔵されていますが、phoneAuthの場合はそうではありません.
このためfirstoreにユーザー情報を入力してログイン時にサーバをロードしようとしますが、セキュリティルール上auth変数なしでサーバにアクセスするべきではありません.
Firebasecloud functionで他のバックエンドサーバなしで行えるようですが、さらに勉強が必要です.
Reference
この問題について(Flutter Firebase Phone Authentication), 我々は、より多くの情報をここで見つけました https://velog.io/@alexcho617/Flutter-Firebase-Phone-Authenticationテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol