Skip to content
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

Apple Sign in #91

Open
pojoba02 opened this issue May 20, 2020 · 1 comment
Open

Apple Sign in #91

pojoba02 opened this issue May 20, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@pojoba02
Copy link

I have tried to implement apple sign in as an authentication provider, am having an issues for
1 Not navigating to the next page
2. It seems is not fetching any data am requesting.
Below is the code
using apple_sign_in 0.1.0 from pub.dev

Future<void> _ensureLoggedIn(BuildContext context) async {
  print('[_ensureLoggedIn] START');

  GoogleSignInAccount user = googleSignIn.currentUser;
  if (user == null) {
    print('[_ensureLoggedIn] user == null');
    print('[_ensureLoggedIn] [signInSilently] START');
    user = await googleSignIn.signInSilently();
    print('[_ensureLoggedIn] [signInSilently] DONE');
  }

  if (user == null) {
    print('[_ensureLoggedIn] user == null');
    print('[_ensureLoggedIn] [signIn] START');
    await googleSignIn.signIn().then((_) {
      tryCreateUserRecord(context);
    });
    print('[_ensureLoggedIn] [signIn] DONE');
  }

  if (await auth.currentUser() == null) {

    print('[_ensureLoggedIn] auth.currentUser() == null');
    print('[_ensureLoggedIn] [googleSignIn.currentUser.authentication] START');


    GoogleSignInAuthentication credentials = await googleSignIn.currentUser.authentication;
    final GoogleSignInAccount googleUser = await googleSignIn.signIn();
    final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

print('[_ensureLoggedIn] [googleSignIn.currentUser.authentication] DONE');
    print('[_ensureLoggedIn] [signInWithGoogle] START');
  final AuthCredential credential = GoogleAuthProvider.getCredential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
 return(await auth.signInWithCredential(credential)).user.uid;
  }
print('[_ensureLoggedIn] DONE');
}
Future<void> _silentLogin(BuildContext context) async {
  GoogleSignInAccount user = googleSignIn.currentUser;
if (user == null) {
    user = await googleSignIn.signInSilently();
    await tryCreateUserRecord(context);
  }
 if (await auth.currentUser() == null && user != null) {
    final GoogleSignInAccount googleUser = await googleSignIn.signIn();
    final GoogleSignInAuthentication googleAuth = await googleUser
        .authentication;
   final AuthCredential credential = GoogleAuthProvider.getCredential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
    await auth.signInWithCredential(credential);
  }
}

// APPLE
Future<void> signInWithApple(BuildContext context) async {
  final AuthorizationResult result = await AppleSignIn.performRequests([
    AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
  ]);
switch (result.status) {
    case AuthorizationStatus.authorized:
 // Store user ID
      await FlutterSecureStorage()
          .write(key: "userId", value: result.credential.user);
       final AppleIdCredential _auth = result.credential;
      final OAuthProvider oAuthProvider = new OAuthProvider(providerId: "apple.com");
     final AuthCredential credential = oAuthProvider.getCredential(
        idToken: String.fromCharCodes(_auth.identityToken),
        accessToken: String.fromCharCodes(_auth.authorizationCode),
      );
 await auth.signInWithCredential(credential);
// update the user information
      if (_auth.fullName != null) {
        auth.currentUser().then( (value) async {
          UserUpdateInfo user = UserUpdateInfo();
          user.displayName = "${_auth.fullName.givenName} ${_auth.fullName.familyName}";
          await value.updateProfile(user);
        });
      }

      break;
 case AuthorizationStatus.error:
      print("Sign In Failed ${result.error.localizedDescription}");
      break;
case AuthorizationStatus.cancelled:
      print("User Cancelled");
      break;
  }
}
tryCreateUserRecord(BuildContext context) async {
  GoogleSignInAccount user = googleSignIn.currentUser;
  if (user == null) {
    return null;
  }
  DocumentSnapshot userRecord = await ref.document(user.id).get();
  if (userRecord.data == null) {
    // no user record exists, time to create
 String userName = await Navigator.push( context,
      // We'll create the SelectionScreen in the next step!
      MaterialPageRoute(
          builder: (context) => Center(
                child: Scaffold(
                    appBar: AppBar(
                      leading: Container(),
                      title: Text('Fill out missing data',
                          style: TextStyle(
                              color: Colors.black,
                              fontWeight: FontWeight.bold)),
                      backgroundColor: Colors.white,
                    ),
                    body: ListView(
                      children: <Widget>[
                         Container(
                          child: CreateAccount(),
                        ),
                      ],
                    )),
              )),
 );
 if (userName != null || userName.length != 0){
      ref.document(user.id).setData({
        "id": user.id,
        "username": userName,
        "photoUrl": user.photoUrl,
        "email": user.email,
        "displayName": user.displayName,
      });
    }
  }
 currentUserModel = User.fromDocument(userRecord);
}
 and separate container for 
AppleSignInButton(
               type: ButtonType.signIn,
                cornerRadius: 6.0,
                 onPressed: () async {
                await signInWithApple( context );
                Navigator.push (context ,MaterialPageRoute(
                    builder: (_) =>
                        HomePage()));
              },

Please any help or any light something on the codes would be appreciated.
Thanks

@mdanics
Copy link
Owner

mdanics commented Jul 3, 2020

Any luck with this? If not, I will implement sign in with Apple in the next few weeks as it is now mandatory.

@mdanics mdanics added the enhancement New feature or request label Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants