davefaliskie / travel_treasury

1ManStartup - Travel Budget Flutter App
http://1manstartup.com
MIT License
308 stars 152 forks source link

Apple signin Issue #2

Closed pojoba02 closed 3 years ago

pojoba02 commented 4 years ago

Hello, have been following your apple sign in config tutorial on youtube for my flutter app, all doe the config does invoke apple sign in IdCredentials, am not getting any data and more over, it doesn't navigator to the next page as it supposed to. Below the code

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 am on a roadblock now, any help or any light something on the codes would be appreciated. Thanks

davefaliskie commented 3 years ago

@pojoba02 sorry just seeing this now (I don't ever check the issues here). Are you testing on an actual device? I don't think Apple sign in will work on the simulator.

In the future commenting on the YouTube videos even with a link to this is a better I read and respond to all those. feel free to email me if you're still having this issue. I'm going to close the issue.