PasswordlessViewController.swift 3.1 KB

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