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?
7 Answers
Use the currentUser()
method:
if (FirebaseAuth.instance.currentUser != null) {
// signed in
} else {
// signed out
}
-
5This 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.– pillar15Commented May 6, 2019 at 11:04
-
2this 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
-
4You can always use
if (await FirebaseAuth.instance.currentUser() == null)
Commented Jul 23, 2019 at 11:19 -
2nullsafety 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
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
}
});
-
9Didn'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
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).
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();
}
-
2When 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. Commented Nov 28, 2019 at 18:31
if (FirebaseAuth.instance.currentUser() != null) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Home(),
);
} else {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Welcome(),
);
}
-
3Please avoid putting code alone as answer and try explain how it solves the issue. Commented Oct 20, 2020 at 9:22
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);
}
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()));
}
});
!= null