skip to Main Content

I am trying to make a notes app, on which i am trying to make user login based on "email and password" method of firebase. I have successfully designed a way to register user with email and password which is also shown on firebase console. Although, when trying to login by using "SignInWithEmailAndPassword" method, i am able to login with unregistered emails and password.

Here is the code,i am having problem in:

 onPressed: () async {
    try {
      final checkuser =
          auth.signInWithEmailAndPassword(
              email: Email!, password: Password!);
      if (checkuser != null) {
        
        Navigator.pushNamed(context, MainScreen.id);
      }
      
    } catch (e) {
      showDialog<String>(
        context: context,
        builder: (BuildContext context) =>
            AlertDialog(
          title: const Text('Login Alert'),
          content: Text(
              e.toString()),
          actions: <Widget>[
            TextButton(
              onPressed: () =>
                  Navigator.pop(context, 'OK'),
              child: const Text('OK'),
            ),
          ],
        ),
      );
    }
  },

Code is running good, there is no syntax error. Only Problem is unregistered users are also allowed to login.

2

Answers


  1. Chosen as BEST ANSWER

    So this was a dumb question, documentation has changed, i guess. The tutorial i followed for creating app has the problematic code that i have written above but as i was going through documentation of firebase thanks to a comment on the questions, i figured out the problem, so here is the updated code:

     onPressed: () async {
                                try {
                                  final checkuser =
                                      await auth.signInWithEmailAndPassword(
                                          email: Email!, password: Password!);
                                  Navigator.pushNamed(context, MainScreen.id);
                                } on FirebaseAuthException catch (e) {
                                  showDialog<String>(
                                    context: context,
                                    builder: (BuildContext context) =>
                                        AlertDialog(
                                      title: const Text('Login Alert'),
                                      content: Text(e.toString()),
                                      actions: <Widget>[
                                        TextButton(
                                          onPressed: () =>
                                              Navigator.pop(context, 'OK'),
                                          child: const Text('OK'),
                                        ),
                                      ],
                                    ),
                                  );
                                }
                              },
    

    So here is the updated code. The code in the question, doesn't have "FirebaseAuthException" block with catch block, so that might be causing problem and letting unregistered user login. Thanks for everyone help!!!


  2. Here is a solution to Firebase login:

        class Root extends StatelessWidget {
          const Root({Key? key}) : super(key: key);
        
          @override
          Widget build(BuildContext context) {
            return StreamBuilder<User?>(
              stream: Authentication.onAuthStateChanged,
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return const CircularProgressIndicator();
                } else if (snapshot.hasError) {
                  return const Center(child: Text('No internet connection!'));
                } else if (snapshot.hasData) {
                  return const HomePage();
                }
                return const LoginPage();
              },
            );
          }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search