UserMetadata.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  16. extension UserMetadata: NSSecureCoding {}
  17. /// A data class representing the metadata corresponding to a Firebase user.
  18. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  19. @objc(FIRUserMetadata) open class UserMetadata: NSObject {
  20. /// Stores the last sign in date for the corresponding Firebase user.
  21. @objc public let lastSignInDate: Date?
  22. /// Stores the creation date for the corresponding Firebase user.
  23. @objc public let creationDate: Date?
  24. init(withCreationDate creationDate: Date?, lastSignInDate: Date?) {
  25. self.creationDate = creationDate
  26. self.lastSignInDate = lastSignInDate
  27. }
  28. // MARK: Secure Coding
  29. private static let kCreationDateCodingKey = "creationDate"
  30. private static let kLastSignInDateCodingKey = "lastSignInDate"
  31. public static var supportsSecureCoding: Bool {
  32. return true
  33. }
  34. public func encode(with coder: NSCoder) {
  35. coder.encode(creationDate, forKey: UserMetadata.kCreationDateCodingKey)
  36. coder.encode(lastSignInDate, forKey: UserMetadata.kLastSignInDateCodingKey)
  37. }
  38. public required convenience init?(coder: NSCoder) {
  39. let creationDate = coder.decodeObject(of: [NSDate.self],
  40. forKey: UserMetadata.kCreationDateCodingKey)
  41. let lastSignInDate = coder.decodeObject(of: [NSDate.self],
  42. forKey: UserMetadata.kLastSignInDateCodingKey)
  43. self.init(withCreationDate: creationDate as? Date, lastSignInDate: lastSignInDate as? Date)
  44. }
  45. }