Reauthenticate user with Facebook

Viewed 298

I am using Flutter to create a mobile application. I am using Firebase for authentication. My user logs in with Facebook. The user should reauthenticate him/her self when deleting the account. The documentation talks about reauthentication using email and password like this:

// Create a credential
EmailAuthCredential credential = EmailAuthProvider.credential(email: email, password: password);

// Reauthenticate
await FirebaseAuth.instance.currentUser.reauthenticateWithCredential(credential);

Since the user does not have an email and password but rather he is logged in with Facebook, I should reauthenticate the user with Facebook. I searched the documentation for this but I had no luck.

Could some one be kind and tell me how to do that.

I tried to log the user in with Facebook (even though he is already logged in) but was not successful. Her is what I tried:

FacebookLoginResult result = await FacebookLogin().logIn(permissions: [
  FacebookPermission.publicProfile,
  FacebookPermission.email,
]);

switch (result.status) {
  case FacebookLoginStatus.success:
    FacebookAccessToken? accessToken = result.accessToken;
    if (accessToken == null) {
      return null;
    }

    UserCredential userCredential = await _auth.signInWithCredential(
      FacebookAuthProvider.credential(accessToken.token),
    );
    return userCredential;

  case FacebookLoginStatus.cancel:
    throw FirebaseAuthException(
      code: 'ERROR_FACEBOOK_LOGIN_FAILED',
      message: result.error?.developerMessage,
    );
  default:
    throw UnimplementedError();
}

await FirebaseAuth.instance.currentUser?.reauthenticateWithCredential(
  userCredential!.credential!,
);
1 Answers

To get Authentication Credential with Google

final GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: <String>[
  'email',
  'https://www.googleapis.com/auth/contacts.readonly',
]);

 final GoogleSignInAccount googleSignInAccount =
    await _googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
    await googleSignInAccount.authentication;

final AuthCredential credential = GoogleAuthProvider.credential(
  accessToken: googleSignInAuthentication.accessToken,
  idToken: googleSignInAuthentication.idToken,
);

To get Authentication Credential with Apple

final appleIdCredential = await SignInWithApple.getAppleIDCredential(
  scopes: [
    AppleIDAuthorizationScopes.email,
    AppleIDAuthorizationScopes.fullName,
  ],
);
final AuthCredential appleCredential =
    OAuthProvider('apple.com').credential(
  accessToken: appleIdCredential.authorizationCode,
  idToken: appleIdCredential.identityToken,
);
Related