[英]How does firebase passwordless authentication work with dynamic link for a flutter app?
When I click on verification link from my email , it opens my app running in background but didChangeAppLifecycleState method returns data as null and deepLink as set in firebase instead of the whole emailLink from my email, resulting in SignInWithEmailAndLink to fail as its supposed to match email address entered in widget from the link recieved.当我单击电子邮件中的验证链接时,它会打开我在后台运行的应用程序,但 didChangeAppLifecycleState 方法将数据返回为空值和在 firebase 中设置的 deepLink,而不是我电子邮件中的整个 emailLink,导致 SignInWithEmailAndLink 失败,因为它应该匹配电子邮件从收到的链接在小部件中输入的地址。
Here's the code taken from this article https://medium.com/firebase-developers/dive-into-firebase-auth-on-flutter-email-and-link-sign-in-e51603eb08f8 :-这是本文的代码https://medium.com/firebase-developers/dive-into-firebase-auth-on-flutter-email-and-link-sign-in-e51603eb08f8 :-
1.didChangeAppLifecycleState method 1.didChangeAppLifecycleState 方法
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed) {
final PendingDynamicLinkData data =
await FirebaseDynamicLinks.instance.getInitialLink();
if( data?.link != null ) {
handleLink(data?.link);
}
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData dynamicLink) async {
final Uri deepLink `enter code here`= dynamicLink?.link;
handleLink(deepLink);
}, onError: (OnLinkErrorException e) async {
print('onLinkError');
print(e.message);
});
}
}
void handleLink(Uri link) async {
if (link != null) {
final User user = (await _auth.signInWithEmailAndLink(
email: _userEmail,
link: link.toString(),
))
.user;
if (user != null) {
setState(() {
_userID = user.uid;
_success = true;
});
} else {
setState(() {
_success = false;
});
}
} else {
setState(() {
_success = false;
});
}
setState(() {});
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
Note: - Deeplink is a concept still new to me in flutter, so I have set it randomly in firebase.注意:-Deeplink 对我来说在 Flutter 中还是一个新概念,所以我在 firebase 中随机设置了它。 My register page has a WidgetsBindingObserver to help resume app lifecycle state.
我的注册页面有一个 WidgetsBindingObserver 来帮助恢复应用程序生命周期状态。 My signup and sign in code is in an email widget and I'm not using forms to validate my textfields.
我的注册和登录代码位于电子邮件小部件中,我没有使用表单来验证我的文本字段。
it seems that after you signin with email link the firebase instance has been not updated before checking auth.SigninWithEMailLink() you need to update it in firebase instance like this似乎在您使用电子邮件链接登录后,在检查 auth.SigninWithEMailLink() 之前,firebase 实例尚未更新,您需要像这样在 firebase 实例中更新它
var user= await auth.currentUser();
await user.reload();
user=await auth.currentUser();
if still you are facing problem please provide some more code to let me understand your issue properly如果您仍然遇到问题,请提供更多代码让我正确理解您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.