UserMetadata.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 let supportsSecureCoding = true
  32. public func encode(with coder: NSCoder) {
  33. coder.encode(creationDate, forKey: UserMetadata.kCreationDateCodingKey)
  34. coder.encode(lastSignInDate, forKey: UserMetadata.kLastSignInDateCodingKey)
  35. }
  36. public required convenience init?(coder: NSCoder) {
  37. let creationDate = coder.decodeObject(of: [NSDate.self],
  38. forKey: UserMetadata.kCreationDateCodingKey)
  39. let lastSignInDate = coder.decodeObject(of: [NSDate.self],
  40. forKey: UserMetadata.kLastSignInDateCodingKey)
  41. self.init(withCreationDate: creationDate as? Date, lastSignInDate: lastSignInDate as? Date)
  42. }
  43. }