EmailAuthProvider.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. let email: String
  38. enum EmailType {
  39. case password(String)
  40. case link(String)
  41. }
  42. let emailType: EmailType
  43. init(withEmail email: String, password: String) {
  44. self.email = email
  45. emailType = .password(password)
  46. super.init(provider: EmailAuthProvider.id)
  47. }
  48. init(withEmail email: String, link: String) {
  49. self.email = email
  50. emailType = .link(link)
  51. super.init(provider: EmailAuthProvider.id)
  52. }
  53. // MARK: Secure Coding
  54. static var supportsSecureCoding = true
  55. func encode(with coder: NSCoder) {
  56. coder.encode(email, forKey: "email")
  57. switch emailType {
  58. case let .password(password): coder.encode(password, forKey: "password")
  59. case let .link(link): coder.encode(link, forKey: "link")
  60. }
  61. }
  62. required init?(coder: NSCoder) {
  63. guard let email = coder.decodeObject(of: NSString.self, forKey: "email") as? String else {
  64. return nil
  65. }
  66. self.email = email
  67. if let password = coder.decodeObject(of: NSString.self, forKey: "password") as? String {
  68. emailType = .password(password)
  69. } else if let link = coder.decodeObject(of: NSString.self, forKey: "link") as? String {
  70. emailType = .link(link)
  71. } else {
  72. return nil
  73. }
  74. super.init(provider: EmailAuthProvider.id)
  75. }
  76. }