From 1789fae541f0e02075735d53aaa5418d87052ddb Mon Sep 17 00:00:00 2001 From: Salakar Date: Sat, 18 Mar 2017 01:10:22 +0000 Subject: [PATCH] [ios][auth] implemented currentUser.updateProfile() method --- ios/RNFirebase/RNFirebaseAuth.m | 124 ++++++++++++-------------------- 1 file changed, 44 insertions(+), 80 deletions(-) diff --git a/ios/RNFirebase/RNFirebaseAuth.m b/ios/RNFirebase/RNFirebaseAuth.m index ae4f1bb8..fbe9427d 100644 --- a/ios/RNFirebase/RNFirebaseAuth.m +++ b/ios/RNFirebase/RNFirebaseAuth.m @@ -4,8 +4,6 @@ @implementation RNFirebaseAuth -typedef void (^UserWithTokenResponse)(NSDictionary *, NSError *); - RCT_EXPORT_MODULE(RNFirebaseAuth); /** @@ -231,6 +229,48 @@ RCT_EXPORT_METHOD(updatePassword:(NSString *) password resolver:(RCTPromiseResol } } +/** + updateProfile + + @param NSDictionary props + @param RCTPromiseResolveBlock resolve + @param RCTPromiseRejectBlock reject + @return return + */ +RCT_EXPORT_METHOD(updateProfile:(NSDictionary *) props resolver:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject) { + FIRUser *user = [FIRAuth auth].currentUser; + + if (user) { + FIRUserProfileChangeRequest *changeRequest = [user profileChangeRequest]; + NSMutableArray *allKeys = [[props allKeys] mutableCopy]; + + for (NSString *key in allKeys) { + @try { + if ([key isEqualToString:@"photoURL"]) { + NSURL *url = [NSURL URLWithString:[props valueForKey:key]]; + [changeRequest setValue:url forKey:key]; + } else { + [changeRequest setValue:[props objectForKey:key] forKey:key]; + } + } @catch (NSException *exception) { + NSLog(@"Exception occurred while configuring: %@", exception); + } + } + + [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) { + if (error) { + [self promiseRejectAuthException:reject error:error]; + } else { + FIRUser *userAfterUpdate = [FIRAuth auth].currentUser; + [self promiseWithUser:resolve rejecter:reject user:userAfterUpdate]; + } + }]; + } else { + [self promiseNoUser:resolve rejecter:reject isError:YES]; + } +} + + /** getToken @@ -393,84 +433,6 @@ RCT_EXPORT_METHOD(reauthenticate:(NSString *)provider authToken:(NSString *)auth } } - -// TODO ------------------------------------------------------- CLEAN UP -------------------------- -// TODO ------------------------------------------------------- CLEAN UP -------------------------- -// TODO ------------------------------------------------------- CLEAN UP -------------------------- -// TODO ------------------------------------------------------- CLEAN UP -------------------------- - -RCT_EXPORT_METHOD(updateUserProfile:(NSDictionary *)userProps - callback:(RCTResponseSenderBlock) callback) -{ - FIRUser *user = [FIRAuth auth].currentUser; - - if (user) { - FIRUserProfileChangeRequest *changeRequest = [user profileChangeRequest]; - - NSMutableArray *allKeys = [[userProps allKeys] mutableCopy]; - for (NSString *key in allKeys) { - // i.e. changeRequest.displayName = userProps[displayName]; - @try { - if ([key isEqualToString:@"photoURL"]) { - NSURL *url = [NSURL URLWithString:[userProps valueForKey:key]]; - [changeRequest setValue:url forKey:key]; - } else { - [changeRequest setValue:[userProps objectForKey:key] forKey:key]; - } - } - @catch (NSException *exception) { - NSLog(@"Exception occurred while configuring: %@", exception); - } - @finally { - [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) { - if (error) { - // An error happened. - [self userErrorCallback:callback error:error user:user msg:@"updateEmailError"]; - } else { - // Profile updated. - [self userCallback:callback user:user]; - } - }]; - } - } - } else { - [self noUserCallback:callback isError:true]; - } -} - -- (void) noUserCallback:(RCTResponseSenderBlock) callback - isError:(Boolean) isError { - if (isError) { - NSDictionary *err = @{ - @"error": @"Unhandled provider" - }; - return callback(@[err]); - - } - return callback(@[[NSNull null], [NSNull null]]); -} - -- (void) userErrorCallback:(RCTResponseSenderBlock) callback - error:(NSError *)error - user:(FIRUser *) user - msg:(NSString *) msg { - // An error happened. - NSDictionary *err = [RNFirebaseErrors handleFirebaseError:msg - error:error - withUser:user]; - callback(@[err]); -} - -- (void) userCallback:(RCTResponseSenderBlock) callback user:(FIRUser *) user { - NSDictionary *userProps = [self firebaseUserToDict:user]; - callback(@[[NSNull null], userProps]); -} - -// END ------------------------------------------------------- CLEAN UP -^ ----------------------- -// END ------------------------------------------------------- CLEAN UP -------------------------- -// END ------------------------------------------------------- CLEAN UP -------------------------- -// END ------------------------------------------------------- CLEAN UP -------------------------- - /** getCredentialForProvider @@ -693,6 +655,8 @@ RCT_EXPORT_METHOD(updateUserProfile:(NSDictionary *)userProps mutableCopy ]; + // todo providerData + if ([user valueForKey:@"photoURL"] != nil) { [userDict setValue: [NSString stringWithFormat:@"%@", user.photoURL] forKey:@"photoURL"]; }