Skip to content

Commit

Permalink
fix(auth, ios): Apple Sign In on a secondary app doesnt sign in the c…
Browse files Browse the repository at this point in the history
…orrect Firebase Auth instance
  • Loading branch information
Lyokone committed Nov 29, 2022
1 parent cba4344 commit 48d4413
Showing 1 changed file with 45 additions and 48 deletions.
Expand Up @@ -69,6 +69,7 @@ @interface FLTFirebaseAuthPlugin ()
@property(strong, nonatomic) FIROAuthProvider *authProvider;
// Used to keep the user who wants to link with Apple Sign In
@property(strong, nonatomic) FIRUser *linkWithAppleUser;
@property(strong, nonatomic) FIRAuth *signInWithAppleAuth;
@property BOOL isReauthenticatingWithApple;
@property(strong, nonatomic) NSString *currentNonce;
@property(strong, nonatomic) FLTFirebaseMethodCallResult *appleResult;
Expand Down Expand Up @@ -601,15 +602,19 @@ - (void)authorizationController:(ASAuthorizationController *)controller
} else if (self.linkWithAppleUser != nil) {
[self.linkWithAppleUser linkWithCredential:credential
completion:^(FIRAuthDataResult *authResult, NSError *error) {
self.linkWithAppleUser = nil;
handleSignInWithApple(self, authResult, error);
}];

} else {
[FIRAuth.auth signInWithCredential:credential
completion:^(FIRAuthDataResult *_Nullable authResult,
NSError *_Nullable error) {
handleSignInWithApple(self, authResult, error);
}];
FIRAuth *signInAuth =
self.signInWithAppleAuth != nil ? self.signInWithAppleAuth : FIRAuth.auth;
[signInAuth signInWithCredential:credential
completion:^(FIRAuthDataResult *_Nullable authResult,
NSError *_Nullable error) {
self.signInWithAppleAuth = nil;
handleSignInWithApple(self, authResult, error);
}];
}
}
}
Expand All @@ -622,21 +627,18 @@ - (void)authorizationController:(ASAuthorizationController *)controller

- (void)signInWithProvider:(id)arguments
withMethodCallResult:(FLTFirebaseMethodCallResult *)result {
FIRAuth *auth = [self getFIRAuthFromArguments:arguments];

if ([arguments[@"signInProvider"] isEqualToString:kSignInMethodApple]) {
if (@available(iOS 13.0, macOS 10.15, *)) {
launchAppleSignInRequest(self, arguments, result);
} else {
NSLog(@"Sign in with Apple was introduced in iOS 13, update your Podfile with platform :ios, "
@"'13.0'");
}
self.signInWithAppleAuth = auth;
launchAppleSignInRequest(self, arguments, result);
return;
}
#if TARGET_OS_OSX
NSLog(@"signInWithProvider is not supported on the "
@"MacOS platform.");
result.success(nil);
#else
FIRAuth *auth = [self getFIRAuthFromArguments:arguments];
self.authProvider = [FIROAuthProvider providerWithProviderID:arguments[@"signInProvider"]];
NSArray *scopes = arguments[kArgumentProviderScope];
if (scopes != nil) {
Expand Down Expand Up @@ -905,29 +907,34 @@ - (void)userGetIdToken:(id)arguments withMethodCallResult:(FLTFirebaseMethodCall

static void launchAppleSignInRequest(FLTFirebaseAuthPlugin *object, id arguments,
FLTFirebaseMethodCallResult *result) {
NSString *nonce = [object randomNonce:32];
object.currentNonce = nonce;
object.appleResult = result;
object.appleArguments = arguments;

ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];

ASAuthorizationAppleIDRequest *request = [appleIDProvider createRequest];
NSMutableArray *requestedScopes = [NSMutableArray arrayWithCapacity:2];
if ([arguments[kArgumentProviderScope] containsObject:@"name"]) {
[requestedScopes addObject:ASAuthorizationScopeFullName];
}
if ([arguments[kArgumentProviderScope] containsObject:@"email"]) {
[requestedScopes addObject:ASAuthorizationScopeEmail];
if (@available(iOS 13.0, macOS 10.15, *)) {
NSString *nonce = [object randomNonce:32];
object.currentNonce = nonce;
object.appleResult = result;
object.appleArguments = arguments;

ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];

ASAuthorizationAppleIDRequest *request = [appleIDProvider createRequest];
NSMutableArray *requestedScopes = [NSMutableArray arrayWithCapacity:2];
if ([arguments[kArgumentProviderScope] containsObject:@"name"]) {
[requestedScopes addObject:ASAuthorizationScopeFullName];
}
if ([arguments[kArgumentProviderScope] containsObject:@"email"]) {
[requestedScopes addObject:ASAuthorizationScopeEmail];
}
request.requestedScopes = [requestedScopes copy];
request.nonce = [object stringBySha256HashingString:nonce];

ASAuthorizationController *authorizationController =
[[ASAuthorizationController alloc] initWithAuthorizationRequests:@[ request ]];
authorizationController.delegate = object;
authorizationController.presentationContextProvider = object;
[authorizationController performRequests];
} else {
NSLog(@"Sign in with Apple was introduced in iOS 13, update your Podfile with platform :ios, "
@"'13.0'");
}
request.requestedScopes = [requestedScopes copy];
request.nonce = [object stringBySha256HashingString:nonce];

ASAuthorizationController *authorizationController =
[[ASAuthorizationController alloc] initWithAuthorizationRequests:@[ request ]];
authorizationController.delegate = object;
authorizationController.presentationContextProvider = object;
[authorizationController performRequests];
}

static void handleAppleAuthResult(FLTFirebaseAuthPlugin *object, id arguments, FIRAuth *auth,
Expand Down Expand Up @@ -977,13 +984,8 @@ - (void)userLinkWithProvider:(id)arguments
}

if ([arguments[@"signInProvider"] isEqualToString:kSignInMethodApple]) {
if (@available(iOS 13.0, macOS 10.15, *)) {
self.linkWithAppleUser = currentUser;
launchAppleSignInRequest(self, arguments, result);
} else {
NSLog(@"Sign in with Apple was introduced in iOS 13, update your Podfile with platform :ios, "
@"'13.0'");
}
self.linkWithAppleUser = currentUser;
launchAppleSignInRequest(self, arguments, result);
return;
}
#if TARGET_OS_OSX
Expand Down Expand Up @@ -1021,13 +1023,8 @@ - (void)reauthenticateWithProvider:(id)arguments
}

if ([arguments[@"signInProvider"] isEqualToString:kSignInMethodApple]) {
if (@available(iOS 13.0, macOS 10.15, *)) {
self.isReauthenticatingWithApple = YES;
launchAppleSignInRequest(self, arguments, result);
} else {
NSLog(@"Sign in with Apple was introduced in iOS 13, update your Podfile with platform :ios, "
@"'13.0'");
}
self.isReauthenticatingWithApple = YES;
launchAppleSignInRequest(self, arguments, result);
return;
}
#if TARGET_OS_OSX
Expand Down

0 comments on commit 48d4413

Please sign in to comment.