J'essaie d'activer l'authentification téléphonique Firebase dans mon projet Flutter afin qu'il envoie un SMS avec un code d'authentification au numéro saisi.
-Téléchargez la clé d'authentification APNs dans les paramètres du projet Firebase sous "Cloud Messaging".
-J'ai aussi ajouté :
io.flutter.embedded_views_preview
<string>NO</string>
au fichier info.plist
J'ai ensuite activé les modes Background -> Remote Notifications et Push Notifications dans le projet Xcode et implémenté ces deux fonctions dans le fichier AppDelegate.swift :
override func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
// Pass device token to auth
Auth.auth().setAPNSToken(deviceToken, type:
AuthAPNSTokenType.prod)
// Further handling of the device token if needed by the app
// ...
}
override func application(_ application: UIApplication,
didReceiveRemoteNotification notification: [AnyHashable :
Any],
fetchCompletionHandler completionHandler: @escaping
(UIBackgroundFetchResult) -> Void) {
if Auth.auth().canHandleNotification(notification) {
completionHandler(UIBackgroundFetchResult.noData)
return
}
// This notification is not auth related, developer should
handle it.
}
C'est le code qui gère l'ensemble du processus d'authentification du téléphone dans l'écran de connexion.
Future<void> verifyPhone() async {
final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verID) {
this.verificationID = verID;
};
final PhoneCodeSent smsCodeSent = (String verID, [int
forceCodeResend]) {
this.verificationID = verID;
smsCodeDialog(context).then((value) {
});
};
final PhoneVerificationCompleted verificationSuccess =
(AuthCredential credential) {
};
final PhoneVerificationFailed verificationFailed =
(AuthException exception) {
print("verification failed this bullshit");
if (exception.message.contains('not authorized'))
print(
'Something weird has gone really wrong, please do not try
later');
else if (exception.message.contains('Network'))
print('Please check your internet connection and try again');
else if (exception.message.contains("credential is invalid"))
print("credential is invalid you jerk");
else
print('Something has gone horribly wrong, please try later or
never -> ${exception.message}');
};
await _auth.verifyPhoneNumber(
phoneNumber: "+" + dialingCode + this.phoneNo,
timeout: const Duration(seconds: 5),
verificationCompleted: verificationSuccess,
verificationFailed: verificationFailed,
codeSent: smsCodeSent,
codeAutoRetrievalTimeout: autoRetrieve);
}
signIn() async {
final AuthCredential credential =
PhoneAuthProvider.getCredential(
verificationId: verificationID,
smsCode: smsCode,
);
final FirebaseUser user = await
_auth.signInWithCredential(credential);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
Navigator.pushReplacementNamed(context, "root");
}
Lorsque je saisis mon numéro de téléphone et que j'appuie sur "envoyer un SMS" sur un iPhone X physique, je m'attends à recevoir un SMS contenant la clé d'authentification, mais je reçois plutôt ces erreurs :
The UIApplicationDelegate must handle remote notification for phone
number authentication to work.
If app delegate swizzling is disabled, remote notifications received
by UIApplicationDelegate need to be forwarded to FIRAuth s
canHandleNotificaton: method.