'Flutter | Getting Firebase Email Link Login Data

I have difficulty implementing the Email Link login with Firebase.

I send the email link using:

_firebaseAuth.sendSignInLinkToEmail(
  email: email,
  actionCodeSettings: ActionCodeSettings(
    url: 'https://subdomain.example.com/user-auth', //<subdomain.example.com> = my real domain
    handleCodeInApp: true,
    androidInstallApp: true,
    androidPackageName: 'com.example.app',
  ),
);

Email is sent and when clicking I open the link using the DynamicLink package:

void _handleDynamicLinks() {
  FirebaseDynamicLinks.instance.onLink(onSuccess: _onSuccess);
}

Future<dynamic> _onSuccess(PendingDynamicLinkData data) async {
  print('---onLink---');
  // How to pass signIn link to `isSignInWithEmailLink` and `signInWithEmailLink` ???
  // data.link returns `https://subdomain.example.com/user-auth` which is not the complete link
}

Every method I call on PendingDynamicLinkData data doesn't return the full dynamic link and isSignInWithEmailLink returns false!



Solution 1:[1]

Try this in your _handleDynamicLink function.

try {
  FirebaseDynamicLinks.instance.onLink.listen((dynamicLink) {
    final Uri? deepLink = dynamicLink.link;
    if (deepLink != null) {
      emailLinkService.handleLink(deepLink, _emailController.text);
      FirebaseDynamicLinks.instance.onLink.listen((dynamicLink) {
        final Uri? deepLink = dynamicLink.link;
        emailLinkService.handleLink(deepLink!, _emailController.text);
      }, onError: (e) async {
        print(e.message);
      });
    }
  }, onError: (e) async {
    print(e.message);
  });
  
  final PendingDynamicLinkData? data =
      await FirebaseDynamicLinks.instance.getInitialLink();
  final Uri? deepLink = data?.link;
  print('deepLink :: $deepLink');
   } catch (e) {
// you can print this error as well
    }

And check if your url is the same as here: enter image description here

And also add the Dynamic link as your custom Authorised domain like this: enter image description here

Here is the handleLink method:

class EmailLinkService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<void> signInWithEmailAndLink(
      {required String userEmail}) async {
    var _userEmail = userEmail;
    var acs = ActionCodeSettings(
        url: Constants.firebaseProjectURL,
        handleCodeInApp: true,
        iOSBundleId: 'com.example....',
        androidPackageName: 'com.example....',
 try {
      return await _auth
          .sendSignInLinkToEmail(email: _userEmail, actionCodeSettings: acs);

    } on FirebaseAuthException catch (e) {

  }

  void handleLink(Uri link, userEmail) async {
    if (link != null) {
      final UserCredential user =
          await FirebaseAuth.instance.signInWithEmailLink(
        email: userEmail,
        emailLink: link.toString(),
      );
     
    } else {
      print(" link is null");
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1