EmailAuthProvider.swift 2.9 KB

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