Home > front end >  Understanding states in Firebase Dynamic links for Authentication
Understanding states in Firebase Dynamic links for Authentication

Time:03-27

I am sorry i have much trouble understanding Firebase Dynamic links.

My use case is : a user wants to reset his password from the mobile app (or send an email verification).

  • The request is made using Firebase Authentication with a custom handler (with custom domain : https://example.com/auth)
  • The ActionCodeSettings looks like :
final ActionCodeSettings codeSettings = ActionCodeSettings(
      url: 'https://links.example.com/auth?email=$email',
      iOSBundleId: Constants.iosBundleID,
      androidPackageName: Constants.androidBundleID,
      androidInstallApp: true,
      dynamicLinkDomain: "links.example.com",
    );
  • The user clicks on the link he received by email and gets redirected to the website (hosted by Firebase Hosting under : example.com)
  • When the user has finished resetting his password, i would expect to redirect him by "launching" the continueUrl that should take him back to the mobile app. continueUrl : 'https://links.example.com/auth?email=$email'

However this doesn't work so i am guessing that i am doing something wrong somewhere.

  1. In my iOS config, i have added the Associated Domains as : applinks:links.example.com.
    In the Info.plist file i have added :
<key>FirebaseDynamicLinksCustomDomains</key>
    <array>
        <string>https://links.example.com/auth</string>
    </array>

(and have also tried with : <string>https://links.example.com</string>)

  1. In my Android config I have added this to my AndroidManifest.xml :
<intent-filter>
               <action android:name="android.intent.action.VIEW"/>
               <category android:name="android.intent.category.DEFAULT"/>
               <category android:name="android.intent.category.BROWSABLE"/>
               <data android:host="links.example.com" android:scheme="https"/>
           </intent-filter>

Fun fact, on Android after the above steps are completed (on website from the smartphone), if i launch the continueUrl it prompts the user whether to redirect back to the app or stay on the browser to open the URL.

I have of course created a sub-domain : links.example.com in the Firebase Dynamic links console as an URL prefix.

Here are my questions :

  • Is the continueUrl supposed to redirect back to the app ?
  • In the ActionCodeSettings continueUrl described above is correct? I see in the documentation always using example.com as the continueUrl, but it would be in conflict with the custom domain used for hosting right ? So i have put links.example.com as the continueUrl and the custom Firebase auth handler is example.com/auth to indeed redirect to the correct web page in my website.
  • What is the Hosting firebase.json configuration for such case ?
  • The final link looks like this :
https://example.com/auth?mode=resetPassword&oobCode=T0qn8aj_p7TJBWyE5eUh7_7ZwIqwtJ7Q-i8LDf4QrIsAAAF_u6Bi6Q&apiKey=AIzaSyAzPqhZFKAyfQDeN4DGGjI9VCTEBe_mLc4&continueUrl=https://links.example.com?link=https://links.example.com/auth?email%[email protected]&apn=com.example.android&amv&ibi=com.example.ios&ifl=https://links.example.com/auth?email%[email protected]&lang=fr
  • Do you see anything wrong or missing ? Something that would prevent the mobile app redirection after the operation completes ?
  • What should I do with the continueUrl param to gets redirected to the app ? Is it automatically done after some event or should the developer writes code to "push" a new web page containing this link and it will see automatically that's not a link to handle in a web page, thus redirect to the mobile app ?

Thanks a lot in advance for any explanations on how this works !

CodePudding user response:

I have finally understood how this works :

The continueUrl must be the one used to handle back in the mobile app so if you use :

url: 'https://links.example.com/auth?email=$email',

This means you have to create a dynamic link prefix URL : https://links.example.com/auth in the firebase console.

You must also add it to your iOS Info.plist file as stated in the question.

Also, when you use a custom domain you need to make sure as stated in the documentation that the URL prefix and the domain are different such as :

https://link.example.com/?link=https://example.com/my-resource

And not :

https://example.com/?link=https://example.com/my-resource

This means that by using https://links.example.com/auth as URL prefix, you need to use another domain to deal with the link.

In my case, i have built the url this way :

https://links.example.com/?link=https://redirect.example.com/auth

And added https://redirect.example.com/auth as URL prefix. This prevent both conflicts between example.com at the hosting level and links.example.com from having both link domain name AND Url prefix identical.

Also, don't forget to add new dynamic links domain as whitelisted domains in the Firebase Authentication Sign-In methods in Firebase console or you will get a domain-denied error.

Hope this will help others to understand better how this works.

  • Related