Apple login

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.4 kB
2
Indexable
Never
  Future loginWithApple() async {
    final RegistrationNotifier registrationNotifier =
        Provider.of<RegistrationNotifier>(
      navigatorKey.currentContext!,
      listen: false,
    );
    try {
      final AuthorizationCredentialAppleID credential =
          await SignInWithApple.getAppleIDCredential(
        scopes: [
          AppleIDAuthorizationScopes.email,
          AppleIDAuthorizationScopes.fullName,
        ],
        webAuthenticationOptions: WebAuthenticationOptions(
          clientId: 'com.nadsoft.arabcast',
          redirectUri: Uri.parse(
            'https://square-pie-marimba.glitch.me/callbacks/sign_in_with_apple',
          ),
        ),
      ).catchError((error) {
        print(error);
      });

      final OAuthProvider oAuthProvider = OAuthProvider('apple.com');

      final OAuthCredential credential1 = oAuthProvider.credential(
        idToken: credential.identityToken,
        accessToken: credential.authorizationCode,
      );

      if (credential != null) {
        print(credential.userIdentifier);
        registrationNotifier.firstNameController.text =
            credential.givenName ?? '';
        registrationNotifier.lastNameController.text =
            credential.familyName ?? '';
        registrationNotifier.emailController!.text = credential.email ?? '';
        registrationNotifier.initPlatformState();
        print('APPLE UID UID: ' + credential.userIdentifier!);
        registrationNotifier.setProviderId(credential.userIdentifier ?? '');
        registrationNotifier.setAccessToken(credential1.accessToken ?? '');
        registrationNotifier.setProvider('ios');

        registrationNotifier.checkUserExist();

        if (credential.email != null && credential.email!.isNotEmpty) {
          final User firebaseUser =
              (await FirebaseAuth.instance.signInWithCredential(credential1))
                  .user!;
        } else {
          print('failed to login , email is required');
        }
      } else {}

      print(credential);
    } catch (ex) {
      print(ex);

      if (ex.toString() ==
          'SignInWithAppleAuthorizationError(AuthorizationErrorCode.canceled, The operation couldn’t be completed. (com.apple.AuthenticationServices.AuthorizationError error 1001.))') {
        print('canceled');
      } else {
        print(ex.toString());
      }
    }
  }