Reputation: 45
I am developing a FLutter web app and using Firebase Authentication with email and password login for signing in users
await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);
After Successfully signing in, the code redirects the user to a HomePage.
Navigator.of(context).pushReplacementNamed('/HomePage');
But when the user refreshes the web page or tries to go to the website again, it has signed out and has to log in again.
Following is the code I use to check the logged in user
StreamBuilder<User?>(
stream: FirebaseAuth.instance.userChanges(),
initialData: FirebaseAuth.instance.currentUser,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.data == null){
Navigator.of(context).pushReplacementNamed('/HomePage');
}else{
Navigator.of(context).pushReplacementNamed('/signup');
}
}
How can I keep the user logged in as long as they use log out? I want the solution to work specifically on flutter web.
Upvotes: 6
Views: 1748
Reputation: 6941
You should navigate to signup when data is null and to home otherwise but you are doing it reverse.
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.data == null){
Navigator.of(context).pushReplacementNamed('/signup');
}else{
Navigator.of(context).pushReplacementNamed('/Homepage');
}
}
Upvotes: 0
Reputation: 252
whatever you are building flutter app, web or desktop. you need to keep recorder of user in share preference. this package shared_preferences: ^2.0.15 support web app too. when user will logout then you must need to remove saved data. Click here:
Upvotes: 2