Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): Access to FIRAuthErrorUserInfoUpdatedCredentialKey with Apple Sign In #4359

Merged
merged 3 commits into from
Oct 7, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/auth/ios/RNFBAuth/RNFBAuthModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

static __strong NSMutableDictionary *authStateHandlers;
static __strong NSMutableDictionary *idTokenHandlers;
// Used for caching credentials between method calls.
static __strong NSMutableDictionary<NSString *, FIRAuthCredential *> *credentials;

@implementation RNFBAuthModule
#pragma mark -
Expand All @@ -67,6 +69,7 @@ - (id)init {
dispatch_once(&onceToken, ^{
authStateHandlers = [[NSMutableDictionary alloc] init];
idTokenHandlers = [[NSMutableDictionary alloc] init];
credentials = [[NSMutableDictionary alloc] init];
});
return self;
}
Expand All @@ -88,6 +91,8 @@ - (void)invalidate {
[[FIRAuth authWithApp:firebaseApp] removeIDTokenDidChangeListener:[idTokenHandlers valueForKey:key]];
}
[idTokenHandlers removeAllObjects];

[credentials removeAllObjects];
}

#pragma mark -
Expand Down Expand Up @@ -922,7 +927,10 @@ - (void)invalidate {
- (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider token:(NSString *)authToken secret:(NSString *)authTokenSecret {
FIRAuthCredential *credential;

if ([provider compare:@"twitter.com" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
// First check if we cached an authToken
if (credentials[authToken] != nil && ![credentials[authToken] isEqual:[NSNull null]]) {
credential = credentials[authToken];
} else if ([provider compare:@"twitter.com" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
credential = [FIRTwitterAuthProvider credentialWithToken:authToken secret:authTokenSecret];
} else if ([provider compare:@"facebook.com" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
credential = [FIRFacebookAuthProvider credentialWithAccessToken:authToken];
Expand Down Expand Up @@ -979,6 +987,7 @@ - (void)promiseRejectAuthException:(RCTPromiseRejectBlock)reject error:(NSError
@"code": [jsError valueForKey:@"code"],
@"message": [jsError valueForKey:@"message"],
@"nativeErrorMessage": [jsError valueForKey:@"nativeErrorMessage"],
@"authCredential": [jsError valueForKey:@"authCredential"],
}];
}

Expand Down Expand Up @@ -1042,10 +1051,17 @@ - (NSDictionary *)getJSError:(NSError *)error {
default:break;
}

NSDictionary *authCredentialDict = nil;
if ([error userInfo][FIRAuthErrorUserInfoUpdatedCredentialKey] != nil) {
FIRAuthCredential *authCredential = [error userInfo][FIRAuthErrorUserInfoUpdatedCredentialKey];
authCredentialDict = [self authCredentialToDict:authCredential];
}

return @{
@"code": code,
@"message": message,
@"nativeErrorMessage": nativeErrorMessage,
@"authCredential" : authCredentialDict != nil ? (id) authCredentialDict : [NSNull null],
};
}

Expand Down Expand Up @@ -1183,6 +1199,19 @@ - (NSDictionary *)firebaseUserToDict:(FIRUser *)user {
};
}

- (NSDictionary*) authCredentialToDict:(FIRAuthCredential *)authCredential {
NSString *authCredentialHash = [NSString stringWithFormat:@"%@", @([authCredential hash])];

// Temporarily store the non-serializable credential for later
credentials[authCredentialHash] = authCredential;

return @{
keyProviderId: authCredential.provider,
@"token": authCredentialHash,
@"secret": [NSNull null],
};
}

- (FIRActionCodeSettings *)buildActionCodeSettings:(NSDictionary *)actionCodeSettings {
FIRActionCodeSettings *settings = [[FIRActionCodeSettings alloc] init];

Expand Down