Skip to content

Commit 673e3e5

Browse files
committed
fix(auth,apple): prevent EXC_BAD_ACCESS crash in Apple Sign-In completion handler
- Locally captures completion handler to prevent deallocation - Fixes crash during async Apple Sign-In operations
1 parent 89d1aa3 commit 673e3e5

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

packages/firebase_auth/firebase_auth/ios/firebase_auth/Sources/firebase_auth/FLTFirebaseAuthPlugin.m

+22-14
Original file line numberDiff line numberDiff line change
@@ -370,19 +370,21 @@ - (NSString *)stringBySha256HashingString:(NSString *)input {
370370

371371
static void handleSignInWithApple(FLTFirebaseAuthPlugin *object, FIRAuthDataResult *authResult,
372372
NSString *authorizationCode, NSError *error) {
373+
void (^completion)(PigeonUserCredential *_Nullable, FlutterError *_Nullable) =
374+
object.appleCompletion;
375+
if (completion == nil) return;
376+
373377
if (error != nil) {
374378
if (error.code == FIRAuthErrorCodeSecondFactorRequired) {
375-
[object handleMultiFactorError:object.appleArguments
376-
completion:object.appleCompletion
377-
withError:error];
379+
[object handleMultiFactorError:object.appleArguments completion:completion withError:error];
378380
} else {
379-
object.appleCompletion(nil, [FLTFirebaseAuthPlugin convertToFlutterError:error]);
381+
completion(nil, [FLTFirebaseAuthPlugin convertToFlutterError:error]);
380382
}
381383
return;
382384
}
383-
object.appleCompletion([PigeonParser getPigeonUserCredentialFromAuthResult:authResult
384-
authorizationCode:authorizationCode],
385-
nil);
385+
completion([PigeonParser getPigeonUserCredentialFromAuthResult:authResult
386+
authorizationCode:authorizationCode],
387+
nil);
386388
}
387389

388390
- (void)authorizationController:(ASAuthorizationController *)controller
@@ -418,6 +420,8 @@ - (void)authorizationController:(ASAuthorizationController *)controller
418420

419421
if (self.isReauthenticatingWithApple == YES) {
420422
self.isReauthenticatingWithApple = NO;
423+
void (^capturedCompletion)(PigeonUserCredential *_Nullable, FlutterError *_Nullable) =
424+
self.appleCompletion;
421425
[[FIRAuth.auth currentUser]
422426
reauthenticateWithCredential:credential
423427
completion:^(FIRAuthDataResult *_Nullable authResult,
@@ -426,16 +430,20 @@ - (void)authorizationController:(ASAuthorizationController *)controller
426430
}];
427431

428432
} else if (self.linkWithAppleUser != nil) {
429-
[self.linkWithAppleUser
430-
linkWithCredential:credential
431-
completion:^(FIRAuthDataResult *authResult, NSError *error) {
432-
self.linkWithAppleUser = nil;
433-
handleSignInWithApple(self, authResult, authorizationCode, error);
434-
}];
433+
FIRUser *userToLink = self.linkWithAppleUser;
434+
void (^capturedCompletion)(PigeonUserCredential *_Nullable, FlutterError *_Nullable) =
435+
self.appleCompletion;
436+
[userToLink linkWithCredential:credential
437+
completion:^(FIRAuthDataResult *authResult, NSError *error) {
438+
self.linkWithAppleUser = nil;
439+
handleSignInWithApple(self, authResult, authorizationCode, error);
440+
}];
435441

436442
} else {
437443
FIRAuth *signInAuth =
438444
self.signInWithAppleAuth != nil ? self.signInWithAppleAuth : FIRAuth.auth;
445+
void (^capturedCompletion)(PigeonUserCredential *_Nullable, FlutterError *_Nullable) =
446+
self.appleCompletion;
439447
[signInAuth signInWithCredential:credential
440448
completion:^(FIRAuthDataResult *_Nullable authResult,
441449
NSError *_Nullable error) {
@@ -468,7 +476,7 @@ - (void)authorizationController:(ASAuthorizationController *)controller
468476
case ASAuthorizationErrorNotHandled:
469477
self.appleCompletion(nil,
470478
[FlutterError errorWithCode:@"not-handled"
471-
message:@"The authorization request wasnt handled."
479+
message:@"The authorization request wasn't handled."
472480
details:nil]);
473481
break;
474482

0 commit comments

Comments
 (0)