No Description

FIRGetAccountInfoResponse.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRGetAccountInfoResponse.h"
  17. #import "FIRAuthErrorUtils.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. /** @var kErrorKey
  20. @brief The key for the "error" value in JSON responses from the server.
  21. */
  22. static NSString *const kErrorKey = @"error";
  23. @implementation FIRGetAccountInfoResponseProviderUserInfo
  24. - (instancetype)initWithDictionary:(NSDictionary *)dictionary {
  25. self = [super init];
  26. if (self) {
  27. _providerID = [dictionary[@"providerId"] copy];
  28. _displayName = [dictionary[@"displayName"] copy];
  29. NSString *photoURL = dictionary[@"photoUrl"];
  30. if (photoURL) {
  31. _photoURL = [NSURL URLWithString:photoURL];
  32. }
  33. _federatedID = [dictionary[@"federatedId"] copy];
  34. _email = [dictionary[@"email"] copy];
  35. _phoneNumber = [dictionary[@"phoneNumber"] copy];
  36. }
  37. return self;
  38. }
  39. @end
  40. @implementation FIRGetAccountInfoResponseUser
  41. - (instancetype)initWithDictionary:(NSDictionary *)dictionary {
  42. self = [super init];
  43. if (self) {
  44. NSArray<NSDictionary *> *providerUserInfoData = dictionary[@"providerUserInfo"];
  45. if (providerUserInfoData) {
  46. NSMutableArray<FIRGetAccountInfoResponseProviderUserInfo *> *providerUserInfoArray =
  47. [NSMutableArray arrayWithCapacity:providerUserInfoData.count];
  48. for (NSDictionary *dictionary in providerUserInfoData) {
  49. [providerUserInfoArray addObject:
  50. [[FIRGetAccountInfoResponseProviderUserInfo alloc] initWithDictionary:dictionary]];
  51. }
  52. _providerUserInfo = [providerUserInfoArray copy];
  53. }
  54. _localID = [dictionary[@"localId"] copy];
  55. _displayName = [dictionary[@"displayName"] copy];
  56. _email = [dictionary[@"email"] copy];
  57. NSString *photoURL = dictionary[@"photoUrl"];
  58. if (photoURL) {
  59. _photoURL = [NSURL URLWithString:photoURL];
  60. }
  61. if ([dictionary[@"createdAt"] isKindOfClass:[NSString class]]) {
  62. // Divide by 1000 in order to convert miliseconds to seconds.
  63. NSTimeInterval creationDateTimeInterval = [dictionary[@"createdAt"] doubleValue] / 1000;
  64. _creationDate = [NSDate dateWithTimeIntervalSince1970:creationDateTimeInterval];
  65. }
  66. if ([dictionary[@"lastLoginAt"] isKindOfClass:[NSString class]]) {
  67. // Divide by 1000 in order to convert miliseconds to seconds
  68. NSTimeInterval creationDateTimeInterval = [dictionary[@"lastLoginAt"] doubleValue] / 1000;
  69. _lastLoginDate = [NSDate dateWithTimeIntervalSince1970:creationDateTimeInterval];
  70. }
  71. _emailVerified = [dictionary[@"emailVerified"] boolValue];
  72. _passwordHash = [dictionary[@"passwordHash"] copy];
  73. _phoneNumber = [dictionary[@"phoneNumber"] copy];
  74. }
  75. return self;
  76. }
  77. @end
  78. @implementation FIRGetAccountInfoResponse
  79. - (BOOL)setWithDictionary:(NSDictionary *)dictionary
  80. error:(NSError *_Nullable *_Nullable)error {
  81. NSArray<NSDictionary *> *usersData = dictionary[@"users"];
  82. // The client side never sends a getAccountInfo request with multiple localID, so only one user
  83. // data is expected in the response.
  84. if (![usersData isKindOfClass:[NSArray class]] || usersData.count != 1) {
  85. if (error) {
  86. *error = [FIRAuthErrorUtils unexpectedResponseWithDeserializedResponse:dictionary];
  87. }
  88. return NO;
  89. }
  90. _users = @[ [[FIRGetAccountInfoResponseUser alloc] initWithDictionary:usersData.firstObject] ];
  91. return YES;
  92. }
  93. @end
  94. NS_ASSUME_NONNULL_END