firebase - How to make sure user is logged in using Firebase and Flutter
问题描述
I want to check if the user is logged in then go to home page, if it is not logged in then go to login page.
The problem I am facing is, after I signed up, if I refresh it directly goes to home page even without logging in. I guess this is because after signing up (createUserWithEmailAndPassword) there is a current user. And I am checking that as the if condition.
Below shown is my code. Can anyone please help me? I want my user to go to home page only after logging in, not after signing up.
doo(context) async {
FirebaseUser user = await FirebaseAuth.instance.currentUser();
if (user == null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LoginPage(),
),
);
} else {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyHomePage(),
),
);
}
}
解决方案
One way you can try to do this is by signing-out the user immediately after the sign-up, something like this..
FirebaseUser user;
user = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: emailController.text,
password: passwordController.text,
);
await FirebaseAuth.instance.signOut();
//then proceed with the logic for navigating to either LoginPage() or HomePage()
//..
推荐阅读
- stm32 - stm32 NVIC_EnableIRQ() 裸机等效?
- vb.net - VB.NET TcpListener 表单不显示
- ios - React Native 无法读取 null 的属性“原型”
- javascript - 基于数组编辑对象中的多个值
- javascript - 如何在 Laravel 中将数据从 html 表保存到数据库
- javascript - 绑定复选框并使用 vuex store 和 vuejs 将所选项目添加到数组中
- google-apps-script - 数据未写入 Google 电子表格
- css - 用 sm 覆盖样式:
- spring - HTTP 状态 500 - 内部服务器错误 Spring Boot
- apache - Apache2 多个域使用相同的文档根