AdditionalUserInfo.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. extension AdditionalUserInfo: NSSecureCoding {}
  16. @objc(FIRAdditionalUserInfo) open class AdditionalUserInfo: NSObject {
  17. /// The provider identifier.
  18. @objc public let providerID: String
  19. /// Dictionary containing the additional IdP specific information.
  20. @objc public let profile: [String: Any]?
  21. /// The name of the user.
  22. @objc public let username: String?
  23. /// Indicates whether or not the current user was signed in for the first time.
  24. @objc public let isNewUser: Bool
  25. // Maintain newUser for Objective-C API.
  26. /// Indicates whether or not the current user was signed in for the first time.
  27. @objc open func newUser() -> Bool {
  28. return isNewUser
  29. }
  30. init(providerID: String, profile: [String: Any]?, username: String?, isNewUser: Bool) {
  31. self.providerID = providerID
  32. self.profile = profile
  33. self.username = username
  34. self.isNewUser = isNewUser
  35. }
  36. // MARK: Secure Coding
  37. private static let providerIDCodingKey = "providerID"
  38. private static let profileCodingKey = "profile"
  39. private static let usernameCodingKey = "username"
  40. private static let newUserKey = "newUser"
  41. public static var supportsSecureCoding: Bool {
  42. return true
  43. }
  44. public required init?(coder aDecoder: NSCoder) {
  45. guard let providerID = aDecoder.decodeObject(
  46. of: NSString.self,
  47. forKey: AdditionalUserInfo.providerIDCodingKey
  48. ) as? String else {
  49. return nil
  50. }
  51. self.providerID = providerID
  52. profile = aDecoder.decodeObject(
  53. of: [NSDictionary.self, NSString.self],
  54. forKey: AdditionalUserInfo.profileCodingKey
  55. ) as? [String: Any]
  56. username = aDecoder.decodeObject(
  57. of: NSString.self,
  58. forKey: AdditionalUserInfo.usernameCodingKey
  59. ) as String?
  60. if let newUser = aDecoder.decodeObject(
  61. of: NSNumber.self,
  62. forKey: AdditionalUserInfo.newUserKey
  63. ) {
  64. isNewUser = newUser.intValue == 1
  65. } else {
  66. isNewUser = false
  67. }
  68. }
  69. public func encode(with aCoder: NSCoder) {
  70. aCoder.encode(providerID, forKey: AdditionalUserInfo.providerIDCodingKey)
  71. aCoder.encode(profile, forKey: AdditionalUserInfo.profileCodingKey)
  72. aCoder.encode(username, forKey: AdditionalUserInfo.usernameCodingKey)
  73. aCoder.encode(isNewUser ? NSNumber(1) : NSNumber(0), forKey: AdditionalUserInfo.newUserKey)
  74. }
  75. }