PasswordlessViewController.swift 2.8 KB

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