1

the whole purpose of this is that i want to signout the current user using app, immediately when i open firebase console and delete his account from Authentication tab.

i want the signout process to be done smoothly without any errors.

what i've tried so far:

in my main function():

runApp(MyApp());

and this is myApp class:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          Provider<AuthService>(
            create: (_) => AuthService(),
          ),
          StreamProvider(
            create: (context) => context.read<AuthService>().onAuthStateChanged,
          ),
        ],
        child: MaterialApp(
          debugShowCheckedModeBanner: false,
          routes: <String, WidgetBuilder>{
            '/home': (BuildContext context) => HomeController(),
            '/signUp': (BuildContext context) => SignUpView(
                  authFormType: AuthFormType.signUp,
                ),
            '/signIn': (BuildContext context) => SignUpView(
                  authFormType: AuthFormType.signIn,
                ),
            '/addGig': (BuildContext context) => Home(passedSelectedIndex: 1),
          },

          home: HomeController(),
        ));
  }
}

and this is the HomeController():

class _HomeControllerState extends State<HomeController> {
  AuthService authService = locator.get<AuthService>();
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final firebaseUserUid = context.watch<String>();
    if (firebaseUserUid != null) {
      return HomePage();
      return MaterialApp(
        debugShowCheckedModeBanner: false,
        routes: <String, WidgetBuilder>{
          '/home': (BuildContext context) => HomeController(),
          '/signUp': (BuildContext context) => SignUpView(
                authFormType: AuthFormType.signUp,
              ),
          '/signIn': (BuildContext context) => SignUpView(
                authFormType: AuthFormType.signIn,
              ),
          '/addGig': (BuildContext context) => Home(passedSelectedIndex: 1),
        },
        theme: fyreworkTheme(),
        builder: EasyLoading.init(),
        home: Home(passedSelectedIndex: 0),
      );
    } else {
      return StartPage();
    }
  }
}

in the build function of MyApp class, the provider is listening to this stream from the AuthService class:

Stream<String> get onAuthStateChanged => _firebaseAuth.onAuthStateChanged.map(
        (FirebaseUser user) => user?.uid,
      );

so far so good...when i start or restart the App...every thing works as intended...no probs.

what i wanted to achieve is that if i open the firebase console / Authentication tab and i know the identifier of a specific user i want to delete his account and i delete it.

i thought that would signout that user from the app and navigates him to StartPage()..as the whole app is listening to onAuthStateChanged stream from the Provider.

but that didn't achieve what i was trying to do.

how can i sign out a specific user from the App after i delete his data from Authentication tab in firebase console ?

i hope i've described the problem and the desired goal well....

any help would be much appreciated.

mohamed mostapha
  • 161
  • 4
  • 16

2 Answers2

0

Whenever the users sends any request to the firebase, revalidate the user to ensure that the user exists in the database or not. You may only if the user exists in the tables or not (not check validity of the tokens). Incase it does not redirect the user to login page. If it does then the user must have authenticated successfully before. This is a very simple solution.

For a complex one, you could use triggers in firebase and send a push notification to the app on deletion of the record of the user. Such that whenever the app receives the push notification that tells it to reauthenticate, the app should assume that the authentication data has been deleted or moved and would redirect the user to the login screen.

Kashish Khullar
  • 394
  • 1
  • 3
  • 14
  • "Whenever the users sends any request to the firebase, revalidate the user to ensure that the user exists in the database or not. "..seems a simple solution yes but i think there would be more clever way to achieve it....i will learn more about push notifications..sounds like it could do the job – mohamed mostapha Feb 13 '21 at 17:47
  • when i see the documentation for onAuthStateChanged...it says "Receive [FirebaseUser] each time the user signIn or signOut"...which means it requires the user to signout intentionally.....is n't there any FirebaseAuth Stream that works autonomously...listen for if user exist or not ? – mohamed mostapha Feb 13 '21 at 17:52
  • Consider this package this might solve your problem: https://pub.dev/packages/firebase_user_stream – Kashish Khullar Feb 13 '21 at 18:29
  • yes the package above seems promising...and also i kept reading through this thread: https://stackoverflow.com/questions/53087895/how-to-force-logout-firebase-auth-user-from-app-remotely – mohamed mostapha Feb 13 '21 at 19:25
0

As an alternative approach, you could use firebase functions to trigger on deleteUser to delete the data server-side... this has the benefit of smaller-client, plus reduces the risk of some 'stuck' conditions should either the data or user delete fail.

exports.onAuthUserDelete = functions.auth.user().onDelete(onAuthUserDelete);

async function onAuthUserDelete(user: admin.auth.UserRecord) {
    await admin.firestore().doc(`/users/${user.uid}`).delete();
    functions.logger.info(`User firestore record deleted: uid=${user.uid}, displayName=${user.displayName}`);
}
Martin
  • 120
  • 7