PasswordlessViewController.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 func sendSignInLink(to email: String) {
  30. let actionCodeSettings = ActionCodeSettings()
  31. // Update "demo" to match the path defined in the dynamic link.
  32. let stringURL = "https://\(authorizedDomain)/demo"
  33. actionCodeSettings.url = URL(string: stringURL)
  34. // The sign-in operation must be completed in the app.
  35. actionCodeSettings.handleCodeInApp = true
  36. actionCodeSettings.setIOSBundleID(Bundle.main.bundleIdentifier!)
  37. AppManager.shared.auth()
  38. .sendSignInLink(toEmail: email, actionCodeSettings: actionCodeSettings) { error in
  39. guard error == nil else { return self.displayError(error) }
  40. // Set `email` property as it will be used to complete sign in after opening email link
  41. self.email = email
  42. print("successfully sent email")
  43. }
  44. }
  45. @objc
  46. private func passwordlessSignIn() {
  47. // Retrieve link that we stored in user defaults in `SceneDelegate`.
  48. guard let link = UserDefaults.standard.value(forKey: "Link") as? String else { return }
  49. AppManager.shared.auth().signIn(withEmail: email, link: link) { result, error in
  50. guard error == nil else { return self.displayError(error) }
  51. guard let currentUser = AppManager.shared.auth().currentUser else { return }
  52. if currentUser.isEmailVerified {
  53. print("User verified with passwordless email.")
  54. self.navigationController?.dismiss(animated: true) {
  55. self.delegate?.loginDidOccur()
  56. }
  57. } else {
  58. print("User could not be verified by passwordless email")
  59. }
  60. }
  61. }
  62. // MARK: - Private Helpers
  63. private func registerForLoginNotifications() {
  64. NotificationCenter.default.addObserver(
  65. self,
  66. selector: #selector(passwordlessSignIn),
  67. name: Notification.Name("PasswordlessEmailNotificationSuccess"),
  68. object: nil
  69. )
  70. }
  71. }