@@ -191,7 +191,8 @@ + (FUIOAuth *)yahooAuthProvider {
191191}
192192
193193+ (FUIOAuth *)appleAuthProvider {
194- return [self appleAuthProviderWithUserInterfaceStyle: UITraitCollection.currentTraitCollection.userInterfaceStyle];
194+ UIUserInterfaceStyle style = UITraitCollection.currentTraitCollection .userInterfaceStyle ;
195+ return [self appleAuthProviderWithUserInterfaceStyle: style];
195196}
196197
197198+ (FUIOAuth *)appleAuthProviderWithUserInterfaceStyle : (UIUserInterfaceStyle)userInterfaceStyle {
@@ -203,6 +204,12 @@ + (FUIOAuth *)appleAuthProviderWithUserInterfaceStyle:(UIUserInterfaceStyle)user
203204 iconImage = [iconImage imageWithTintColor: [UIColor blackColor ]];
204205 buttonColor = [UIColor whiteColor ];
205206 buttonTextColor = [UIColor blackColor ];
207+ } else if (userInterfaceStyle == UIUserInterfaceStyleLight) {
208+ iconImage = [iconImage imageWithTintColor: [UIColor whiteColor ]];
209+ buttonColor = [UIColor blackColor ];
210+ buttonTextColor = [UIColor whiteColor ];
211+ } else {
212+ iconImage = [iconImage imageWithTintColor: [UIColor whiteColor ]];
206213 }
207214 FUIOAuth *provider = [[FUIOAuth alloc ] initWithAuthUI: [FUIAuth defaultAuthUI ]
208215 providerID: @" apple.com"
@@ -213,7 +220,7 @@ + (FUIOAuth *)appleAuthProviderWithUserInterfaceStyle:(UIUserInterfaceStyle)user
213220 scopes: @[@" name" , @" email" ]
214221 customParameters: nil
215222 loginHintKey: nil ];
216- provider.buttonAlignment = FUIButtonAlignmentCenter ;
223+ provider.buttonAlignment = FUIButtonAlignmentLeading ;
217224 provider.buttonTextColor = buttonTextColor;
218225 return provider;
219226}
@@ -310,13 +317,47 @@ - (BOOL)handleOpenURL:(NSURL *)URL sourceApplication:(nullable NSString *)source
310317
311318#pragma mark - ASAuthorizationControllerDelegate
312319
320+ + (NSPersonNameComponentsFormatter *)nameFormatter {
321+ static NSPersonNameComponentsFormatter *nameFormatter;
322+ static dispatch_once_t onceToken;
323+ dispatch_once (&onceToken, ^{
324+ nameFormatter = [[NSPersonNameComponentsFormatter alloc ] init ];
325+ });
326+ return nameFormatter;
327+ }
328+
313329- (void )authorizationController : (ASAuthorizationController *)controller didCompleteWithAuthorization : (ASAuthorization *)authorization API_AVAILABLE(ios(13.0 )) {
314- ASAuthorizationAppleIDCredential* appleIDCredential = authorization.credential ;
330+ ASAuthorizationAppleIDCredential *appleIDCredential = authorization.credential ;
331+ NSData *rawIdentityToken = appleIDCredential.identityToken ;
332+ if (rawIdentityToken == nil ) {
333+ // It's pretty awful to not have an error when login is unsuccessful, but Apple's docs
334+ // don't provide any useful information here.
335+ // https://developer.apple.com/documentation/authenticationservices/asauthorizationappleidcredential
336+ NSLog (@" Sign in with Apple completed with authorization, but no jwt: %@ " , authorization);
337+ _providerSignInCompletion (nil , nil , nil , nil );
338+ }
315339 NSString *idToken = [[NSString alloc ] initWithData: appleIDCredential.identityToken encoding: NSUTF8StringEncoding];
316340 FIROAuthCredential *credential = [FIROAuthProvider credentialWithProviderID: @" apple.com"
317341 IDToken: idToken
318342 accessToken: nil ];
319- _providerSignInCompletion (credential, nil , nil , nil );
343+ FIRAuthResultCallback result;
344+ NSPersonNameComponents *nameComponents = appleIDCredential.fullName ;
345+ if (nameComponents != nil ) {
346+ NSPersonNameComponentsFormatter *formatter = [[self class ] nameFormatter ];
347+ NSString *displayName = [formatter stringFromPersonNameComponents: nameComponents];
348+ NSDictionary *userInfo = @{FIRAuthCredentialDisplayName: displayName};
349+ credential.userInfo = userInfo;
350+
351+ result = ^(FIRUser *_Nullable user,
352+ NSError *_Nullable error) {
353+ if (user != nil ) {
354+ FIRUserProfileChangeRequest *displayNameUpdate = [user profileChangeRequest ];
355+ displayNameUpdate.displayName = displayName;
356+ [displayNameUpdate commitChangesWithCompletion: ^(NSError * _Nullable error) {}];
357+ }
358+ };
359+ }
360+ _providerSignInCompletion (credential, nil , result, nil );
320361}
321362
322363- (void )authorizationController : (ASAuthorizationController *)controller didCompleteWithError : (NSError *)error API_AVAILABLE(ios(13.0 )) {
0 commit comments