AuthDataResult.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /** @class AuthDataResult
  16. @brief Helper object that contains the result of a successful sign-in, link and reauthenticate
  17. action. It contains references to a `User` instance and a `AdditionalUserInfo` instance.
  18. */
  19. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  20. @objc(FIRAuthDataResult) public class AuthDataResult: NSObject, NSSecureCoding {
  21. /** @property user
  22. @brief The signed in user.
  23. */
  24. @objc public let user: User
  25. /** @property additionalUserInfo
  26. @brief If available contains the additional IdP specific information about signed in user.
  27. */
  28. @objc public let additionalUserInfo: AdditionalUserInfo?
  29. /** @property credential
  30. @brief This property will be non-nil after a successful headful-lite sign-in via
  31. `signIn(with:uiDelegate:completion:)`. May be used to obtain the accessToken and/or IDToken
  32. pertaining to a recently signed-in user.
  33. */
  34. @objc public let credential: OAuthCredential?
  35. private let kAdditionalUserInfoCodingKey = "additionalUserInfo"
  36. private let kUserCodingKey = "user"
  37. private let kCredentialCodingKey = "credential"
  38. // TODO: All below here should be internal
  39. /** @fn initWithUser:additionalUserInfo:
  40. @brief Designated initializer.
  41. @param user The signed in user reference.
  42. @param additionalUserInfo The additional user info.
  43. @param credential The updated OAuth credential if available.
  44. */
  45. @objc public init(withUser user: User,
  46. additionalUserInfo: AdditionalUserInfo?,
  47. credential: OAuthCredential? = nil) {
  48. self.user = user
  49. self.additionalUserInfo = additionalUserInfo
  50. self.credential = credential
  51. }
  52. public static var supportsSecureCoding: Bool {
  53. return true
  54. }
  55. public func encode(with coder: NSCoder) {
  56. coder.encode(user, forKey: kUserCodingKey)
  57. coder.encode(additionalUserInfo, forKey: kAdditionalUserInfoCodingKey)
  58. coder.encode(credential, forKey: kCredentialCodingKey)
  59. }
  60. public required init?(coder: NSCoder) {
  61. guard let user = coder.decodeObject(forKey: kUserCodingKey) as? User else {
  62. return nil
  63. }
  64. self.user = user
  65. additionalUserInfo = coder.decodeObject(forKey: kAdditionalUserInfoCodingKey)
  66. as? AdditionalUserInfo
  67. credential = coder.decodeObject(forKey: kCredentialCodingKey) as? OAuthCredential
  68. }
  69. }