signInWithApple method

Future<User?> signInWithApple({
  1. required String clientId,
  2. required String redirectUri,
})

Implementation

Future<User?> signInWithApple({required String clientId, required String redirectUri}) async {
  try {
    // Ensure that the `redirectUri` is correct and well-formed
    final Uri parsedRedirectUri = Uri.parse(redirectUri);

    // Obtain Apple credentials, handling web and non-web scenarios
    final appleCredential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        AppleIDAuthorizationScopes.email,
        AppleIDAuthorizationScopes.fullName,
      ],
      // Web authentication options for platforms like Android or web
      webAuthenticationOptions: WebAuthenticationOptions(
        clientId: clientId,
        redirectUri: parsedRedirectUri,
      ),
    );

    // Create OAuth credentials from Apple credentials
    final oauthCredential = OAuthProvider("apple.com").credential(
      idToken: appleCredential.identityToken,
      accessToken: appleCredential.authorizationCode,
    );

    // Sign in with the created credentials
    UserCredential result = await _auth.signInWithCredential(oauthCredential);

    return result.user;
  } catch (e) {
    // Print the error for debugging purposes
    print("Error during Apple sign-in: ${e.toString()}");
    return null;
  }
}