jesper
jesper

Reputation: 395

Flutter Firebase - failing to properly delete a Google authenticated user

I'm trying but failing to re-trigger the authentication steps that the user gets taken through when they authenticate themselves using Google sign-in, following deletion of the user. The deleted user simply gets signed in immediately (instead of being taken through the authentication steps), when using Google sign-in the second time. I want to be able to re-trigger the authentication steps for my own testing purposes.

Specifically, I've got a user who I've authenticated and signed in as per the FlutterFire documentation, i.e.

Future<UserCredential> signInWithGoogle() async {
  // Trigger the authentication flow
  final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();

  // Obtain the auth details from the request
  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

  // Create a new credential
  final GoogleAuthCredential credential = GoogleAuthProvider.credential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );

  // Once signed in, return the UserCredential
  return await FirebaseAuth.instance.signInWithCredential(credential);
}

I then proceed to delete the user; again, as per the FlutterFire documentation, i.e.

try {
  await FirebaseAuth.instance.currentUser.delete();
} catch on FirebaseAuthException (e) {
  if (e.code == 'requires-recent-login') {
    print('The user must reauthenticate before this operation can be executed.');
  }
}

That works, insomuch as the user is no longer listed amongst the authenticated users in the Firebase console. However, if I now proceed to call signInWithGoogle() again, then instead of getting taken through the authentication steps again (i.e. being prompted to enter an email, password, etc.), the user simply gets signed in straight away. It's as if the user hasn't been properly deleted. How would I go about re-triggering the authentication steps?

Upvotes: 1

Views: 1639

Answers (2)

Sambasten
Sambasten

Reputation: 259

In my case, I had to reauthenticate firebase user inside the delete functions try-catch as currentUser() always return null AND GoogleSignIn().signOut() didnt work. Maybe a bug.

 import 'package:google_sign_in/google_sign_in.dart';
 import 'package:firebase_auth/firebase_auth.dart';

 final GoogleSignIn _googleSignIn = GoogleSignIn();
 final FirebaseAuth _auth = FirebaseAuth.instance;

//will need to sign in to firebase auth again as currentUser always returns null
//this try-catch block should be inside the function that deletes user

try {
  //FirebaseUser user = await _auth.currentUser(); //returns null so useless

  //signin to google account again
  GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();
  GoogleSignInAuthentication googleSignInAuthentication =
      await googleSignInAccount.authentication;
  //get google credentials
  AuthCredential credential = GoogleAuthProvider.getCredential(
      idToken: googleSignInAuthentication.idToken,
      accessToken: googleSignInAuthentication.accessToken);

  //use credentials to sign in to Firebase
  AuthResult authResult = await _auth.signInWithCredential(credential);
  //get firebase user
  FirebaseUser user = authResult.user;
  print(user.email);
  //delete user
  await user.delete();
  //signout from google sign in
  await _googleSignIn.signOut();
} catch (e) {
  print('Failed to delete user ' + e.toString());
}

Upvotes: 0

Lee3
Lee3

Reputation: 3067

You must also call GoogleSignIn().signOut() after the Firebase sign out or delete.

Upvotes: 3

Related Questions