From 5b7de35bb2a66294970ee8aa493f200bbcfd6791 Mon Sep 17 00:00:00 2001 From: Chaitanya Bhagvan Date: Fri, 23 Dec 2016 15:36:19 +0530 Subject: [PATCH 1/2] Maintain similar response format for getCurrentUser and signInWith* --- ios/Firestack/FirestackAuth.m | 37 +++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/ios/Firestack/FirestackAuth.m b/ios/Firestack/FirestackAuth.m index 1ebccec..6d45075 100644 --- a/ios/Firestack/FirestackAuth.m +++ b/ios/Firestack/FirestackAuth.m @@ -17,7 +17,7 @@ @implementation FirestackAuth RCT_EXPORT_MODULE(FirestackAuth); RCT_EXPORT_METHOD(signInAnonymously: - (RCTResponseSenderBlock) callBack) + (RCTResponseSenderBlock) callback) { @try { [[FIRAuth auth] signInAnonymouslyWithCompletion @@ -32,10 +32,14 @@ @implementation FirestackAuth [self sendJSEvent:AUTH_CHANGED_EVENT props: evt]; - callBack(@[evt]); + callback(@[evt]); } else { - NSDictionary *userProps = [self userPropsFromFIRUser:user]; - callBack(@[[NSNull null], userProps]); + NSDictionary *userProps = [self userPropsFromFIRUser:user]; + NSDictionary *responseProps = @{ + @"authenticated": @((BOOL) true), + @"user": userProps + }; + callback(@[[NSNull null], responseProps]); } }]; } @catch(NSException *ex) { @@ -47,7 +51,7 @@ @implementation FirestackAuth [self sendJSEvent:AUTH_ERROR_EVENT props:eventError]; NSLog(@"An exception occurred: %@", ex); - callBack(@[eventError]); + callback(@[eventError]); } } @@ -61,7 +65,11 @@ @implementation FirestackAuth if (user != nil) { NSDictionary *userProps = [self userPropsFromFIRUser:user]; - callback(@[[NSNull null], userProps]); + NSDictionary *responseProps = @{ + @"authenticated": @((BOOL) true), + @"user": userProps + }; + callback(@[[NSNull null], responseProps]); } else { NSDictionary *err = [FirestackErrors handleFirebaseError:AUTH_ERROR_EVENT @@ -94,7 +102,11 @@ @implementation FirestackAuth if (user != nil) { // User is signed in. NSDictionary *userProps = [self userPropsFromFIRUser:user]; - callback(@[[NSNull null], userProps]); + NSDictionary *responseProps = @{ + @"authenticated": @((BOOL) true), + @"user": userProps + }; + callback(@[[NSNull null], responseProps]); } else { NSLog(@"An error occurred: %@", [error localizedDescription]); NSLog(@"[Error signInWithProvider]: %@", [error userInfo]); @@ -146,7 +158,7 @@ @implementation FirestackAuth sendJSEvent:AUTH_CHANGED_EVENT props: @{ @"eventName": @"userTokenError", - @"authenticated": @((BOOL)true), + @"authenticated": @((BOOL)false), @"errorMessage": [error localizedFailureReason] }]; } else { @@ -236,10 +248,11 @@ @implementation FirestackAuth completion:^(FIRUser *user, NSError *error) { if (user != nil) { NSDictionary *userProps = [self userPropsFromFIRUser:user]; - - callback(@[[NSNull null], @{ - @"user": userProps - }]); + NSDictionary *responseProps = @{ + @"authenticated": @((BOOL) true), + @"user": userProps + }; + callback(@[[NSNull null], responseProps]); } else { NSDictionary *err = [FirestackErrors handleFirebaseError:@"signinError" From 47cc17eb7b4b692c95d2100c6c4f18b97c237010 Mon Sep 17 00:00:00 2001 From: Chaitanya Bhagvan Date: Fri, 23 Dec 2016 15:36:51 +0530 Subject: [PATCH 2/2] Remove anonymousUsercallback --- .../io/fullstack/firestack/FirestackAuth.java | 46 ++----------------- 1 file changed, 5 insertions(+), 41 deletions(-) diff --git a/android/src/main/java/io/fullstack/firestack/FirestackAuth.java b/android/src/main/java/io/fullstack/firestack/FirestackAuth.java index 34e2bf5..489ce5b 100644 --- a/android/src/main/java/io/fullstack/firestack/FirestackAuth.java +++ b/android/src/main/java/io/fullstack/firestack/FirestackAuth.java @@ -170,7 +170,7 @@ public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { FirestackAuthModule.this.user = task.getResult().getUser(); - anonymousUserCallback(FirestackAuthModule.this.user, callback); + userCallback(FirestackAuthModule.this.user, callback); } else { // userErrorCallback(task, callback); } @@ -523,14 +523,15 @@ public void userCallback(FirebaseUser passedUser, final Callback callback) { public void onComplete(@NonNull Task task) { WritableMap msgMap = Arguments.createMap(); WritableMap userMap = getUserMap(); + boolean authenticated = false; if (FirestackAuthModule.this.user != null) { final String token = task.getResult().getToken(); userMap.putString("token", token); - userMap.putBoolean("anonymous", false); + authenticated = true; } msgMap.putMap("user", userMap); - msgMap.putBoolean("authenticated", true); + msgMap.putBoolean("authenticated", authenticated); callback.invoke(null, msgMap); } }).addOnFailureListener(new OnFailureListener() { @@ -541,43 +542,6 @@ public void onFailure(@NonNull Exception ex) { }); } - // TODO: Reduce to one method - public void anonymousUserCallback(FirebaseUser passedUser, final Callback callback) { - - if (passedUser == null) { - mAuth = FirebaseAuth.getInstance(); - this.user = mAuth.getCurrentUser(); - } else { - this.user = passedUser; - } - - this.user.getToken(true) - .addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - WritableMap msgMap = Arguments.createMap(); - WritableMap userMap = getUserMap(); - - if (FirestackAuthModule.this.user != null) { - final String token = task.getResult().getToken(); - - userMap.putString("token", token); - userMap.putBoolean("anonymous", true); - } - - msgMap.putMap("user", userMap); - - callback.invoke(null, msgMap); - } - }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception ex) { - userExceptionCallback(ex, callback); - } - }); - } - - public void noUserCallback(final Callback callback) { WritableMap message = Arguments.createMap(); @@ -624,7 +588,7 @@ private WritableMap getUserMap() { userMap.putString("uid", uid); userMap.putString("providerId", provider); userMap.putBoolean("emailVerified", user.isEmailVerified()); - + userMap.putBoolean("anonymous", user.isAnonymous()); if (name != null) { userMap.putString("displayName", name); }