'Firebase Authentication link not working - missing trailing slash?

I want to use Firebase email link authentication in my iOS (Flutter) app with a custom domain:

  1. The app requests Firebase Auth backend to send an email to the user. -- works

  2. The user clicks a link in the email and is taken back to the app. -- works

  3. The app processes the payload of that link to perform the sign in. -- does not work

Firebase Dynamic Links do work in general but the link from that particular Firebase auto-generated email is not. Technically the app is brought to foreground but the Firebase Dynamic Link onLink callback is not fired. I tracked it down to one tiny trailing slash that is missing in the email's link URL:

https://link.mydomain.com?link=https://app.mydomain.com/__/auth/action?apiKey=... (not working)

https://link.mydomain.com/?link=https://app.mydomain.com/__/auth/action?apiKey=... (works)
-------------------------^

To work around this I tried to add a path suffix to my domain but this gave the same results:

https://link.mydomain.com/app?link=https://app.mydomain.com/__/auth/action?apiKey=... (not working)

https://link.mydomain.com/app/?link=https://app.mydomain.com/__/auth/action?apiKey=... (works)
-----------------------------^

The worst part of this difference that made it so hard to find is that if you copy the link on iOS or long press it to see a preview, the slash is just silently added in the right place! You can paste the link to the notes app, click it, and it works!

Only when viewing the email's source code you can see that it is actually not there:

Screenshot from email source code

When requesting the link, I call FirebaseAuth.instance.sendSignInWithEmailLink with these params set:

url: 'https://app.mydomain.com',
dynamicLinkDomain: 'link.mydomain.com',

The dynamicLinkDomain param does not accept a protocol or path.

Does anybody know how to...

  • make Firebase add this slash to the link when sending the mail?

or

  • make Firebase Dynamic Link's onLink callback react to clicked links that do not have a trailing slash?

For reference

My Info.plist contains this key:

<key>FirebaseDynamicLinksCustomDomains</key>
<array>
    <string>https://link.mydomain.com</string>
</array>

My entitlements file contains this key:

<key>com.apple.developer.associated-domains</key>
<array>
    <string>applinks:link.mydomain.com</string>
</array>

apple-app-site-association (https://link.mydomain.com/apple-app-site-association):

{
  "applinks": {
    "apps": [],
    "details": [{
      "appID": "XYZ.com.mydomain.app",
      "paths": [
        "NOT /_/*",
        "/*"
      ]
    }]
  }
}


Solution 1:[1]

After debugging the iOS implementation of Firebase Dynamic Links it turns out that the missing slash is explicitly required in code (though I am not sure why). Its absence in the email link seems to be a bug and should probably be fixed in the Firebase backend; I filed a bug report on that. The Firebase console has no option to configure a dynamic link domain to be used for automatically generated sign-in links (instead takes any first from the list of configured domains but somehow without path) so I hope this will be made configurable in the future.

Workaround

I simply changed that code (in my Flutter project it's in Pods/FirebaseDynamicLinks/FIRDynamicLinks.m) and removed the check for the trailing slash. But I have to repeat it when the dependency is updated and I don't know why the check is explicitly there so it might cause other problems.

Two more hints on debugging your dynamic links

  1. Check the link itself

Copy the link into the address line of your browser and add &d=1 to get the debug view. It shows errors in a big red box above of the diagram - if there are any.

  1. Check your iOS configuration

Add these two lines temporarily to your didFinishLaunchingWithOptions in AppDelegate.m:

[FIRApp configure];
[FIRDynamicLinks performDiagnosticsWithCompletion:nil];

(you may have to @import Firebase;)

Run the project in Xcode to see a report like this in the debug console:

---- Firebase Dynamic Links diagnostic output start ----
Firebase Dynamic Links framework version 4.0.8
System information: OS iOS, OS version 14.0.1, model iPhone
Current date 2020-10-02 05:02:39 +0000
Device locale en-US (raw en_US), timezone Europe/Berlin
    Specified custom URL scheme is com.domain and Info.plist contains such scheme in CFBundleURLTypes key.
    AppID Prefix: XXXXXXXX, Team ID: XXXXXXXX, AppId Prefix equal to Team ID: YES
performDiagnostic completed successfully! No errors found.
---- Firebase Dynamic Links diagnostic output end ----

Solution 2:[2]

The links with a custom domain worked fine in Apple Mail but no other email client.

As a temporary solution, you can use a Google-provided domain (e.g. projectname.page.link) to generate sign-in links.

You can track it here.

Solution 3:[3]

This seems to have been fix in later firebase sdks, if you look at the PR linked in this github issue (I haven't tested, but I have an older sdk version with the issue)

If you don't want to update your SDK right away, an alternative solution is to not use firebase auto-generated email, and instead provide your own email where you insert the firebase link.

In that case, you can tweak the link before inserting it in the email, and make sure it has a trailing slash.

Solution 4:[4]

dont forget to add dynamicLinkDomain: 'link.mydomain.com',

url: 'https://app.mydomain.com',
dynamicLinkDomain: 'link.mydomain.com'

I got it working after adding dynamicLinkDomain: 'link.mydomain.com'

P.S Flutter project

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
Solution 2 Allan Wolski
Solution 3 D0m3
Solution 4 sultanmyrza