EmailAuthProvider.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2023 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 Foundation
  15. /**
  16. @brief A concrete implementation of `AuthProvider` for Email & Password Sign In.
  17. */
  18. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  19. @objc(FIREmailAuthProvider) open class EmailAuthProvider: NSObject {
  20. @objc public static let id = "password"
  21. /**
  22. @brief Creates an `AuthCredential` for an email & password sign in.
  23. @param email The user's email address.
  24. @param password The user's password.
  25. @return An `AuthCredential` containing the email & password credential.
  26. */
  27. @objc open class func credential(withEmail email: String, password: String) -> AuthCredential {
  28. return EmailAuthCredential(withEmail: email, password: password)
  29. }
  30. /** @fn credentialWithEmail:Link:
  31. @brief Creates an `AuthCredential` for an email & link sign in.
  32. @param email The user's email address.
  33. @param link The email sign-in link.
  34. @return An `AuthCredential` containing the email & link credential.
  35. */
  36. @objc open class func credential(withEmail email: String, link: String) -> AuthCredential {
  37. return EmailAuthCredential(withEmail: email, link: link)
  38. }
  39. }
  40. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  41. @objc(FIREmailPasswordAuthCredential) class EmailAuthCredential: AuthCredential, NSSecureCoding {
  42. let email: String
  43. enum EmailType {
  44. case password(String)
  45. case link(String)
  46. }
  47. let emailType: EmailType
  48. init(withEmail email: String, password: String) {
  49. self.email = email
  50. emailType = .password(password)
  51. super.init(provider: EmailAuthProvider.id)
  52. }
  53. init(withEmail email: String, link: String) {
  54. self.email = email
  55. emailType = .link(link)
  56. super.init(provider: EmailAuthProvider.id)
  57. }
  58. // MARK: Secure Coding
  59. static var supportsSecureCoding = true
  60. func encode(with coder: NSCoder) {
  61. coder.encode(email, forKey: "email")
  62. switch emailType {
  63. case let .password(password): coder.encode(password, forKey: "password")
  64. case let .link(link): coder.encode(link, forKey: "link")
  65. }
  66. }
  67. required init?(coder: NSCoder) {
  68. guard let email = coder.decodeObject(of: NSString.self, forKey: "email") as? String else {
  69. return nil
  70. }
  71. self.email = email
  72. if let password = coder.decodeObject(of: NSString.self, forKey: "password") as? String {
  73. emailType = .password(password)
  74. } else if let link = coder.decodeObject(of: NSString.self, forKey: "link") as? String {
  75. emailType = .link(link)
  76. } else {
  77. return nil
  78. }
  79. super.init(provider: EmailAuthProvider.id)
  80. }
  81. }