PhoneAuthViewController.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 PhoneAuthViewController: OtherAuthViewController {
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. configureUI(for: .PhoneNumber)
  20. }
  21. override func buttonTapped() {
  22. guard let phoneNumber = textField.text, !phoneNumber.isEmpty else { return }
  23. phoneAuthLogin(phoneNumber)
  24. }
  25. // MARK: - Firebase 🔥
  26. private func phoneAuthLogin(_ phoneNumber: String) {
  27. let phoneNumber = String(format: "+%@", phoneNumber)
  28. PhoneAuthProvider.provider()
  29. .verifyPhoneNumber(phoneNumber, uiDelegate: nil) { verificationID, error in
  30. guard error == nil else { return self.displayError(error) }
  31. guard let verificationID = verificationID else { return }
  32. self.presentPhoneAuthController { verificationCode in
  33. let credential = PhoneAuthProvider.provider()
  34. .credential(withVerificationID: verificationID, verificationCode: verificationCode)
  35. self.signin(with: credential)
  36. }
  37. }
  38. }
  39. private func signin(with credential: PhoneAuthCredential) {
  40. AppManager.shared.auth().signIn(with: credential) { result, error in
  41. guard error == nil else { return self.displayError(error) }
  42. self.navigationController?.dismiss(animated: true, completion: {
  43. self.delegate?.loginDidOccur()
  44. })
  45. }
  46. }
  47. private func presentPhoneAuthController(saveHandler: @escaping (String) -> Void) {
  48. let phoneAuthController = UIAlertController(
  49. title: "Sign in with Phone Auth",
  50. message: nil,
  51. preferredStyle: .alert
  52. )
  53. phoneAuthController.addTextField { textfield in
  54. textfield.placeholder = "Enter verification code."
  55. textfield.textContentType = .oneTimeCode
  56. }
  57. let onContinue: (UIAlertAction) -> Void = { _ in
  58. let text = phoneAuthController.textFields!.first!.text!
  59. saveHandler(text)
  60. }
  61. phoneAuthController
  62. .addAction(UIAlertAction(title: "Continue", style: .default, handler: onContinue))
  63. phoneAuthController.addAction(UIAlertAction(title: "Cancel", style: .cancel))
  64. present(phoneAuthController, animated: true, completion: nil)
  65. }
  66. }