1

I am using flutter_bloc and firebase for phone auth.

I am facing this strange behavior where in , When i delete user account manually, and restart application , Firebase.instance.currentUser still exists from the previous login!!!!

How to overcome this!

class AuthCubit extends Cubit<AuthState> {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  // Firebase
  AuthCubit() : super(AuthInitialState()) {
    User? currentUser = _auth.currentUser;

    print("The current user is $currentUser");
    print("the current user's uid is : ${currentUser?.uid}");
    print("The current user is ${currentUser?.phoneNumber}");
   
  }

This will generate the following output:

I/flutter (11633): The current user is User(displayName: , email: , emailVerified: false, isAnonymous: false, metadata: UserMetadata(creationTime: 2023-01-21 06:10:04.712Z, lastSignInTime: 2023-01-21 06:10:04.713Z), phoneNumber: +919535334951, photoURL: null, providerData, [UserInfo(displayName: , email: , phoneNumber: +91XXXXXXXX, photoURL: null, providerId: phone, uid: )], refreshToken: , tenantId: null, uid: JhSqO5pZsMbp3vgZA6s1WqjKTU63)
I/flutter (11633): the current user's uid is : JhSqO5pZsMbp3vgZA6s1WqjKTU63
I/flutter (11633): The current user is +91XXXXXXXX

My firebase auth section looks like:

enter image description here

  • Just a hunch, but maybe this is a local cache on the flutter app itself. Perhaps you can [clear cache](https://stackoverflow.com/a/58902735/16683394) or check with the server every startup whether the user data is still there. – Ping34 Jan 21 '23 at 06:26
  • But in every restart I am checking the server ! and clear cache isn't working :( – Master Techie Jan 21 '23 at 06:37
  • Try signOut the current user. – user18309290 Jan 21 '23 at 07:19

1 Answers1

0

You are probably using an iphone which stores the user session in it's own keystore(unrelated to the app), so deleting the app/app data doesn't clear the session.

Unfortunately the only way is to add a line of code to sign out the user at the start of the app and then remove it.

RailTracer
  • 101
  • 1
  • 7
  • I am using emulator and android phone , and about the user session cache, I tried deleting , even then it didn't work, only after i uninstall and reinstall the app it works , what is happening – Master Techie Jan 21 '23 at 09:40
  • deleting app data should work, but I mean why can't you just logout the user? – RailTracer Jan 21 '23 at 09:56
  • But I mean, why can't just deleting the user from the firebase work ? What is happening actually behind the scenes – Master Techie Jan 21 '23 at 10:46
  • 1
    As far as I know firebase caches your login for 30 minutes. When you delete on firebase you on delete on the server, but your login is still cached. That's why you either have to logout, remove all app data, or remove the app. Or you can wait 30 minutes. – RailTracer Jan 21 '23 at 15:21