Firebase Email verification not working with ActionCodeSetting

ε祈祈猫儿з 提交于 2019-12-02 19:16:43

问题


I'm trying to implement verification of a user's email (with the default verification URL in the email template), AND an ActionCodeSetting URL (dynamic link) to bring the user back to the app. I'm baffled by how Firebase's email verification with ActionCodeSetting is supposed to work. I have read every available page of documentation and it is still unclear to me how to properly configure the "continue URL" to NOT preempt and override the default verification URL.

What I have done:

  1. I tested the email verification with the automatically generated email verification link. It worked.
  2. I then added an ActionCodeSetting URL that uses a domain that is added to the Associated Domains of the XCode project. This worked to bring the user back to the app after clicking the verification link.
  3. Problem: the email verification no longer works.

Here is the code I have implemented:

var actionCodeSettings = ActionCodeSettings.init()
                actionCodeSettings.handleCodeInApp = true
                let user = Auth.auth().currentUser
                let urlString = "https://blaproject.page.link/zCB4"
                actionCodeSettings.setIOSBundleID(Bundle.main.bundleIdentifier!)
                actionCodeSettings.setAndroidPackageName("com.example.android", installIfNotAvailable:true, minimumVersion:"12")

                Auth.auth().currentUser?.sendEmailVerification(with: actionCodeSettings, completion: { (error) in
                    print("verification email sent")
                    print("action code setting URL is: \(String(describing: actionCodeSettings.url))")
                })

Here is the default verification URL from the email template in the Firebase console:

https://blaproject-ea9d6.firebaseapp.com/__/auth/action?mode=&oobCode=

And here is the verification URL that gets sent by the above code:

https://blaproject.page.link?link=https://blaproject-ea9d6.firebaseapp.com//auth/action?apiKey%3DAIzaSyAi1fxd-HdkfXzYJxTpwmB3_mVCy5gvWxA%26mode%3DverifyEmail%26oobCode%3DqjvGoqc1n3ya0OIi_tWIYTpp59DYKgB6Sbj0EymN2IkAAAFkYNutMA%26continueUrl%3Dhttps://blaproject.page.link/zCE4%26lang%3Den&apn=com.example.android&amv=12&ibi=blaproject.blaprojectV0-2&ifl=https://blaproject-ea9d6.firebaseapp.com//auth/action?apiKey%3DAIzaSyAi1fxd-HdkfXzYJxTpwmB3_mVCy5gvWxA%26mode%3DverifyEmail%26oobCode%3DqjvGoqc1n3ya0OIi_tWIYTpp59DYKgB6Sbj0EymN2IkAAAFkYNutMA%26continueUrl%3Dhttps://blaproject.page.link/zCE4%26lang%3Den

So my question is, why does this URL not verify the user's email and then use the continue URL (and the associated domain) to trigger the app to open? It only triggers the app to open, without verifying the user's email.

Thanks for any tips you can provide to help me understand what I'm not understanding :)


回答1:


When the link triggers your app to open. You need to parse the oobCode from the deep link. You can use FDL client library to get the deep link. Refer to the following documentation on the format of the deep link and how to parse the code: https://firebase.google.com/docs/auth/custom-email-handler

Once you get the code, you need to apply it using the auth.applyActionCode(code) API. This will verify the email.

After verification, you can call user.reload() to update the emailVerified property on the user. You can also force user.getIdToken(true) to force refresh the token with the updated verified email if you are using Firebase security rule.



来源:https://stackoverflow.com/questions/51178015/firebase-email-verification-not-working-with-actioncodesetting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!