EmailAuthProvider.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 public 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 public 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. 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. public static var supportsSecureCoding = true
  59. public func encode(with coder: NSCoder) {
  60. coder.encode(email, forKey: "email")
  61. switch emailType {
  62. case let .password(password): coder.encode(password, forKey: "password")
  63. case let .link(link): coder.encode(link, forKey: "link")
  64. }
  65. }
  66. public required init?(coder: NSCoder) {
  67. guard let email = coder.decodeObject(of: NSString.self, forKey: "email") as? String else {
  68. return nil
  69. }
  70. self.email = email
  71. if let password = coder.decodeObject(of: NSString.self, forKey: "password") as? String {
  72. emailType = .password(password)
  73. } else if let link = coder.decodeObject(of: NSString.self, forKey: "link") as? String {
  74. emailType = .link(link)
  75. } else {
  76. return nil
  77. }
  78. super.init(provider: EmailAuthProvider.id)
  79. }
  80. }