Flutter Apple sign in not returning name always

26 Views Asked by At

This is my sign-in with Apple function

signInWithApple(Ref ref) async {
    try {
      final FirebaseAuth auth = FirebaseAuth.instance;
      final AuthorizationCredentialAppleID appleCredential =
          await SignInWithApple.getAppleIDCredential(
        scopes: [
          AppleIDAuthorizationScopes.email,
          AppleIDAuthorizationScopes.fullName
        ],
        webAuthenticationOptions: WebAuthenticationOptions(
          clientId: 'clientId',
          redirectUri: Uri.parse('uri'),
        ),
      );
      final OAuthProvider oAuthProvider = OAuthProvider('apple.com');
      final credential = oAuthProvider.credential(
          idToken: appleCredential.identityToken,
          accessToken: appleCredential.authorizationCode);
      final User? user = (await auth.signInWithCredential(credential)).user;
      if (user?.email != null) {
        ref
            .read(authProvider.notifier)
            .setAppleEmailToRegistration(user!.email!);
        final Either<CleanFailure, EmailLoginSecondStepResponse> data =
            await api.post(
          fromData: (json) => EmailLoginSecondStepResponse.fromMap(json),
          endPoint: APIRoute.socialLogin,
          body: {"email": user.email},
          withToken: false,
        );
        return data;
      } else {
        return left(
          CleanFailure.withData(
            tag: "Apple sign-in error",
            url: "apple.com",
            method: "POST",
            statusCode: 000,
            header: const {},
            body: const {},
            error: const CleanError(
              message: "Apple sign-in error",
            ),
          ),
        );
      }
    } catch (e) {
      Logger.e("Apple sign in error: $e");
      return left(
        CleanFailure.withData(
          tag: "Apple sign-in error",
          url: "apple.com",
          method: "POST",
          statusCode: 000,
          header: const {},
          body: const {},
          error: CleanError(
            message: e.toString(),
          ),
        ),
      );
    }
  }

The issue lies in retrieving the full name from Apple credentials. Currently, only the email is accessible. However, the full name is obtained only upon the user's re-login to their Apple account. If an Apple account remains logged in on the device, the name field returns null.

Can anyone help me to fix this?

1

There are 1 best solutions below

0
Zeelkumar Sojitra On

Please try to implementing this code to solve your error.

static Future<UserCredential?> signInWithApple() async {
try {
  final result = await SignInWithApple.getAppleIDCredential(
    scopes: [
      AppleIDAuthorizationScopes.email,
      AppleIDAuthorizationScopes.fullName,
    ],
  );
  if (result == null) {
    return null;
  }
  // Check if the user canceled the sign-in
  if (result.authorizationCode == null && result.identityToken == null) {
    log('Apple Sign-In canceled by user.');
    return null;
  }

  final oauthCredential = OAuthProvider("apple.com").credential(
    idToken: result.identityToken,
    accessToken: result.authorizationCode,
  );
  if (result.email != null) {
    HiveUtils.set(HiveKeys.loginEmail, result.email);
    HiveUtils.set(HiveKeys.loginDisplayName, result.givenName);

    if (kDebugMode) {
      log('Successfully signed in with Google: ${result.givenName}');
      log('User data saved to Hive: ${result.email}, ${result.givenName}');
    }
  } else {
    if (kDebugMode) {
      log('Google Sign-In failed.');
    }
  }
  return await _auth.signInWithCredential(oauthCredential);
} on SignInWithAppleAuthorizationException catch (e) {
  // Handle specific exception for Apple Sign In
  debugPrint('AppleSignInError: $e');
  return null;
} on PlatformException catch (e) {
  // Handle other platform exceptions
  debugPrint('AppleSignInError: $e');
  return null;
} catch (e) {
  // Handle generic exception
  log("Error signing in with Apple: $e");
  debugPrint('AppleSignInError: $e');
  return null;
}
}