0

Hello I have this function to log in with facebook, but it returned a UserCredential, is it possible to convert it to User from the firebase_auth package?

Future<UserCredential> facebook() async {
try {
  final AccessToken accessToken = await FacebookAuth.instance.login();

  // Create a credential from the access token
  final FacebookAuthCredential credential = FacebookAuthProvider.credential(
    accessToken.token,
  );
  // Once signed in, return the UserCredential
  UserCredential authResult =
      await _firebaseAuth.signInWithCredential(credential);

} on FacebookAuthException catch (e) {
  // handle the FacebookAuthException
} on FirebaseAuthException catch (e) {
  // handle the FirebaseAuthException
} finally {}
return null; 
}
Talked
  • 173
  • 1
  • 11

1 Answers1

2

UserCredential actually has a getter for User.

      final AccessToken accessToken = await FacebookAuth.instance.login();
      final AuthCredential credential =
          FacebookAuthProvider.getCredential(accessToken: accessToken.token);
      final firebaseUser = (await auth.signInWithCredential(credential)).user;
Stijn2210
  • 812
  • 6
  • 14
  • Why the #$%! is this complicated nest of function/property calls required?! According to much older questions/answers on SO, this used to be possible directly from the return value of all auth related functions, so why did Google feel the need to change that?! Anyway sorry for the rant, and thank you for saving me from putting a bullet in my head LOL. +1 – Kenny83 Sep 03 '21 at 08:31
  • haha, you're welcome! You're not the only one that thinks this is overcomplicated ;) – Stijn2210 Sep 03 '21 at 10:39