简体   繁体   English

firebase 无密码身份验证如何与 Flutter 应用程序的动态链接一起使用?

[英]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);
    });
  }
}
  1. handleLink method handleLink 方法
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(() {});
}
  1. main method (initializing firebase)主要方法(初始化firebase)
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.

相关问题 Expo 应用程序中的 Firebase 无密码电子邮件身份验证错误 - Firebase Passwordless Email Authentication Error in Expo App Flutter:可以在单独的隔离工作中初始化 Firebase 动态链接、远程配置、身份验证和深层链接吗? - Flutter: Can initialize Firebase dynamic link, remote config, authentication, and deep link in a separate isolate work? 如果应用程序已经在运行,Flutter 如何获取 firebase 动态链接? - Flutter how to get firebase dynamic link if app is already running? 是否可以通过 Firebase 无密码身份验证发送 OTP 而不是链接? - Is it possible to send an OTP instead of a link via Firebase Passwordless authentication? Flutter Firebase Email 无密码登录 - 链接错误 - Flutter Firebase Email Passwordless login - Link giving error Firebase 无密码电子邮件身份验证不会在 iOS 上打开应用程序 - Firebase Passwordless Email Authentication doesn't open app on iOS Flutter 上的 Firebase 动态链接 - Firebase Dynamic Link on Flutter Firebase 与 flutter 动态链接:深层链接 URL 未打开应用程序 - Firebase dynamic link with flutter: Deep link URL doesnt open app Flutter 和 Firebase,无密码电子邮件身份验证? - Flutter & Firebase, passwordless email auth? Flutter App如何在Firebase中集成身份验证和数据库? - How to integrate authentication and database in Firebase for Flutter App?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM