Skip to content

fix(auth,apple): prevent EXC_BAD_ACCESS crash in Apple Sign-In completion handler #17273

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,21 @@ - (NSString *)stringBySha256HashingString:(NSString *)input {

static void handleSignInWithApple(FLTFirebaseAuthPlugin *object, FIRAuthDataResult *authResult,
NSString *authorizationCode, NSError *error) {
void (^completion)(PigeonUserCredential *_Nullable, FlutterError *_Nullable) =
object.appleCompletion;
if (completion == nil) return;

if (error != nil) {
if (error.code == FIRAuthErrorCodeSecondFactorRequired) {
[object handleMultiFactorError:object.appleArguments
completion:object.appleCompletion
withError:error];
[object handleMultiFactorError:object.appleArguments completion:completion withError:error];
} else {
object.appleCompletion(nil, [FLTFirebaseAuthPlugin convertToFlutterError:error]);
completion(nil, [FLTFirebaseAuthPlugin convertToFlutterError:error]);
}
return;
}
object.appleCompletion([PigeonParser getPigeonUserCredentialFromAuthResult:authResult
authorizationCode:authorizationCode],
nil);
completion([PigeonParser getPigeonUserCredentialFromAuthResult:authResult
authorizationCode:authorizationCode],
nil);
}

- (void)authorizationController:(ASAuthorizationController *)controller
Expand Down Expand Up @@ -418,6 +420,8 @@ - (void)authorizationController:(ASAuthorizationController *)controller

if (self.isReauthenticatingWithApple == YES) {
self.isReauthenticatingWithApple = NO;
void (^capturedCompletion)(PigeonUserCredential *_Nullable, FlutterError *_Nullable) =
self.appleCompletion;
[[FIRAuth.auth currentUser]
reauthenticateWithCredential:credential
completion:^(FIRAuthDataResult *_Nullable authResult,
Expand All @@ -426,16 +430,20 @@ - (void)authorizationController:(ASAuthorizationController *)controller
}];

} else if (self.linkWithAppleUser != nil) {
[self.linkWithAppleUser
linkWithCredential:credential
completion:^(FIRAuthDataResult *authResult, NSError *error) {
self.linkWithAppleUser = nil;
handleSignInWithApple(self, authResult, authorizationCode, error);
}];
FIRUser *userToLink = self.linkWithAppleUser;
void (^capturedCompletion)(PigeonUserCredential *_Nullable, FlutterError *_Nullable) =
self.appleCompletion;
[userToLink linkWithCredential:credential
completion:^(FIRAuthDataResult *authResult, NSError *error) {
self.linkWithAppleUser = nil;
handleSignInWithApple(self, authResult, authorizationCode, error);
}];

} else {
FIRAuth *signInAuth =
self.signInWithAppleAuth != nil ? self.signInWithAppleAuth : FIRAuth.auth;
void (^capturedCompletion)(PigeonUserCredential *_Nullable, FlutterError *_Nullable) =
self.appleCompletion;
[signInAuth signInWithCredential:credential
completion:^(FIRAuthDataResult *_Nullable authResult,
NSError *_Nullable error) {
Expand Down
Loading