EmailAuthProvider.swift 3.2 KB

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