24

I'm using firebase authentication to authenticate users in my Flutter app ,when the user entered his right password the app shows him the home page normally but when the password is wrong doesn't happened nothing, I want to show an alert every time the user enters a wrong password, How can I do this?

3
  • Check if the current user is != null Commented Jun 6, 2018 at 6:37
  • @GünterZöchbauer Can you write all the code please
    – STEP PLUS
    Commented Jun 6, 2018 at 6:45
  • write your code so we can know what you are using Commented Jun 6, 2018 at 7:00

7 Answers 7

37

Use the currentUser() method:

if (FirebaseAuth.instance.currentUser != null) {
    // signed in
} else {
   // signed out   
}
5
  • 5
    This didn't work for me, probably as currentUser returns a future, which is not null and would always make the app think the user is signed in. With await or .then can actually get the user object and not the future.
    – pillar15
    Commented May 6, 2019 at 11:04
  • 2
    this answer is not correct FirebaseAuth.instance.currentUser() is async in firebase_auth plugin 0.11.0, perhaps in the past it was synchronous Commented May 13, 2019 at 15:26
  • 4
    You can always use if (await FirebaseAuth.instance.currentUser() == null) Commented Jul 23, 2019 at 11:19
  • 2
    nullsafety issue... error: The function can't be unconditionally invoked because it can be 'null' Commented Dec 21, 2021 at 8:07
  • This is now a getter, so drop the (): firebase.google.com/docs/auth/flutter/… Commented Jan 11, 2023 at 18:33
14

That didn't work for me so this is what I did:

FirebaseAuth.instance.currentUser().then((firebaseUser){
  if(firebaseUser == null)
   {
     //signed out
   }
   else{
    //signed in
  }
});
2
  • 9
    Didn't work for me. Firebase user is always valid even when it's being deleted. Commented May 7, 2019 at 7:43
  • 3
    @OliverDixon I'm having exactly the same issue. Did you find any solution? Commented Nov 12, 2019 at 10:19
6

Maybe the shortest and safest answer is:

if(FirebaseAuth.instance.currentUser?.uid == null){
// not logged
} else {
// logged
}

It gets the User ID (if exists) which is recorded on Firebase Authentication UID field for each user have been authenticated on the app before, otherwise it returns null.
The question mark in currentUser?.uid is a Null safety to avoid null exception (returns null if null).

3

try this, it worked for me.

Get token from user and refresh it.

Future<bool> isUserLogged() async {
    FirebaseUser firebaseUser = await getLoggedFirebaseUser();
    if (firebaseUser != null) {
        IdTokenResult tokenResult = await firebaseUser.getIdToken(refresh: true);
        return tokenResult.token != null;
    } else {
        return false;
    }
}

Future<FirebaseUser> getLoggedFirebaseUser() {
    return firebaseAuthInstance().currentUser();
}
1
  • 2
    When answering an old question, your answer would be much more useful to other StackOverflow users if you included some context to explain how your answer helps. See: How do I write a good answer.
    – David Buck
    Commented Nov 28, 2019 at 18:31
1
if (FirebaseAuth.instance.currentUser() != null) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Home(),
    );
} else {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Welcome(),
    );
}
1
  • 3
    Please avoid putting code alone as answer and try explain how it solves the issue. Commented Oct 20, 2020 at 9:22
0

void _emailLogin() async{ final user = await _auth.signInWithEmailAndPassword(email: email, password: password);

    if(user != null){
     // Do something
    }
  } catch (e) {

    String exception = Auth.getExceptionText(e);
    Flushbar(
      title: "Sign In Error",
      message: exception,
      duration: Duration(seconds: 5),
    )..show(context);

  }
0

In Your Init Method Do This..

 FirebaseAuth.instance.currentUser().then((firebaseUser) {
    if (firebaseUser == null) {
     
      Navigator.pushReplacement(
          context,
          MaterialPageRoute(
              builder: (BuildContext context) => LoginPage()));
    } else {
      Navigator.pushReplacement(context,
          MaterialPageRoute(builder: (BuildContext context) => HomePage()));
    }
  });

Not the answer you're looking for? Browse other questions tagged or ask your own question.