Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect back to my app after the user has verified their email using Firebase Auth and Swift?

When a user signs up in my app, they get a pop up that says, please verify your email and then login. When the user clicks OK, it takes them to login page. At this point the user should go to the Mail app (iPhone) and click the link that has been sent to them from Firebase. Clicking this link currently opens Safari and tells the user the account is verified, then the user has to manually go back to the app. How can I verify the user AND then also take them back to the app?

I've read up about continueURL and also the documentation that Firebase provides but it just doesn't make any sense and I'm getting confused on what I need to do. If someone can explain how to do this is in simpler terms that would be much appreciated.

This is how I'm sending out the email: user?.sendEmailVerification(completion: nil)

Thank you in advance for your help!

like image 973
Sahil Kotecha Avatar asked Oct 28 '17 18:10

Sahil Kotecha


People also ask

How does Firebase email verification work?

You can use Firebase Authentication to sign in a user by sending them an email containing a link, which they can click to sign in. In the process, the user's email address is also verified. There are numerous benefits to signing in by email: Low friction sign-up and sign-in.

How do I use Firebase authentication email?

If you haven't yet connected your app to your Firebase project, do so from the Firebase console. Enable Email/Password sign-in: In the Firebase console, open the Auth section. On the Sign in method tab, enable the Email/password sign-in method and click Save.

How do I create a verification email link?

To generate an email verification link, provide the existing user's unverified email and an optional ActionCodeSettings object. The operation will resolve with the email action link. The email used must belong to an existing user. // Admin SDK API to generate the email verification link.

Where you can edit the email template of email address verification in Firebase?

Open your project in the Firebase console. Go to the Email Templates page in the Auth section. In any of the Email Types entries, click the pencil icon to edit the email template.


Video Answer


1 Answers

you can have the link directly take you to the iOS app but handle the code in the app by setting handleCodeInApp to YES. For simplicity, let's illustrate how to first open the link in the web page (handleCodeInApp is NO which is the default), verify the email and then redirect back to the mobile app to continue to the user's intended destination.

var actionCodeSettings =  ActionCodeSettings.init()
actionCodeSettings.canHandleInApp = false
let user = Auth.auth().currentUser()
// This URL will be the deep link of the FDL. It is useful for
// passing state back to your iOS app to let it know that you were
// verifying a user of email user.email. This is also useful
// in case the user clicks the continue button a non iOS device.
// You should own this link.
actionCodeSettings.URL =
    String(format: "https://www.example.com/?email=%@", user.email)
// This is your iOS app bundle ID. It will try to redirect to your
// app via Firebase dynamic link.
actionCodeSettings.setIOSBundleID("com.example.ios")
user.sendEmailVerification(withActionCodeSettings:actionCodeSettings { error in
  if error {
    // Error occurred. Inspect error.code and handle error.
    return
  }
  // Email verification sent.
})

In the above flow, the verification link will be sent to the user. User will click the link. It will open the provisioned web page where the email will be verified. A continue button is shown which on click, if the iOS app is installed on the device, will redirect back to the app.

You will use Firebase dynamic link to intercept that link. Learn more about configuring FDL in iOS and handling the link here:

https://firebase.google.com/docs/dynamic-links/ios/receive https://firebase.google.com/docs/auth/ios/passing-state-in-email-actions#configuring_firebase_dynamic_links

The Firebase dynamic link will have a deep link with the following URL: https://www.example.com/?email=%@", user.email

like image 150
bojeil Avatar answered Sep 22 '22 13:09

bojeil