PasswordlessViewController.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseAuth
  15. import UIKit
  16. class PasswordlessViewController: OtherAuthViewController {
  17. private var email: String!
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. configureUI(for: .Passwordless)
  21. registerForLoginNotifications()
  22. }
  23. override func buttonTapped() {
  24. guard let email = textField.text, !email.isEmpty else { return }
  25. sendSignInLink(to: email)
  26. }
  27. // MARK: - Firebase 🔥
  28. private let authorizedDomain: String = "ENTER AUTHORIZED DOMAIN"
  29. private let customDomain: String = "ENTER AUTHORIZED HOSTING DOMAIN"
  30. private func sendSignInLink(to email: String) {
  31. let actionCodeSettings = ActionCodeSettings()
  32. // Update "demo" to match the path defined in the dynamic link.
  33. let stringURL = "https://\(authorizedDomain)/demo"
  34. actionCodeSettings.url = URL(string: stringURL)
  35. // The sign-in operation must be completed in the app.
  36. actionCodeSettings.handleCodeInApp = true
  37. actionCodeSettings.setIOSBundleID(Bundle.main.bundleIdentifier!)
  38. actionCodeSettings.linkDomain = customDomain
  39. AppManager.shared.auth()
  40. .sendSignInLink(toEmail: email, actionCodeSettings: actionCodeSettings) { error in
  41. guard error == nil else { return self.displayError(error) }
  42. // Set `email` property as it will be used to complete sign in after opening email link
  43. self.email = email
  44. print("successfully sent email")
  45. }
  46. }
  47. @objc
  48. private func passwordlessSignIn() {
  49. // Retrieve link that we stored in user defaults in `SceneDelegate`.
  50. guard let link = UserDefaults.standard.value(forKey: "Link") as? String else { return }
  51. AppManager.shared.auth().signIn(withEmail: email, link: link) { result, error in
  52. guard error == nil else { return self.displayError(error) }
  53. guard let currentUser = AppManager.shared.auth().currentUser else { return }
  54. if currentUser.isEmailVerified {
  55. print("User verified with passwordless email.")
  56. self.navigationController?.dismiss(animated: true) {
  57. self.delegate?.loginDidOccur()
  58. }
  59. } else {
  60. print("User could not be verified by passwordless email")
  61. }
  62. }
  63. }
  64. // MARK: - Private Helpers
  65. private func registerForLoginNotifications() {
  66. NotificationCenter.default.addObserver(
  67. self,
  68. selector: #selector(passwordlessSignIn),
  69. name: Notification.Name("PasswordlessEmailNotificationSuccess"),
  70. object: nil
  71. )
  72. }
  73. }