我有一个两步登录表单,用户首先输入电子邮件,然后进入下一页,在其中输入密码。在将用户带到密码页面之前,我想检查用户输入的电子邮件是否有效以及它是否存在于 firebase 中。我读过一些关于使用fetchSignInMethodsForEmail. 但是,我不太确定如何实现这一点。
这个想法是,如果电子邮件确实存在,则用户将被带到密码页面,但是,如果电子邮件不存在,则会弹出一个带有按钮的小吃栏,将用户带到注册页面。我该怎么做呢?
这就是我到目前为止所拥有的......
FirebaseAuth auth = FirebaseAuth.instance;
final nextButton = Container(
margin: const EdgeInsets.only(
top: 30.0,
),
child: FloatingActionButton(
onPressed: () async {
try {
List<String> userSignInMethods = await auth.fetchSignInMethodsForEmail(email)(
email: _emailController.text,
);
if (userSignInMethods != null) {
final page = LoginPassword();
Navigator.of(context).push(CustomPageRoute(page));
}
} catch (e) {
print(e);
//TODO: Snackbar
}
},
tooltip: 'Next',
backgroundColor: AppTheme.define().primaryColor,
child: Icon(
AppIcons.next_button,
size: 20.0,
),
),
);Run Code Online (Sandbox Code Playgroud)
未fetchSignInMethodsForEmail正确实施,我收到错误。
这可能不是您想要的,但我通常所做的是将用户存储在 firestore 的集合中,因为这允许我为用户存储多个值,如昵称、图片、积分等。如果您想要一些示例代码如何做到这一点,请告诉我
编辑
这是一些帮助您入门的代码。
首先设置 firestore 并设置一个名为 users 的集合。使用您的邮件 ID 在其中创建一个文档,并在其中添加您想要为用户提供的字段。如果不这样做,您将收到错误消息,指出找不到集合或未设置 firestore
注册时,鉴于电子邮件位于可变电子邮件和密码中,请执行类似的操作
FirebaseFirestore.instance.collection('users').doc(email).set((){
"email":email,
"pass":pass,
"nick":nick,//You can add more fields or remove them
});
Run Code Online (Sandbox Code Playgroud)
当您想检查电子邮件是否存在时,请执行以下操作
QuerySnapshot query = await FirebaseFirestore.instance.collection('users').where('email',isEqualTo:email).get();
if (query.docs.length==0){
//Go to the sign up screen
}
else {
//Go to the login screen
}
Run Code Online (Sandbox Code Playgroud)
现在要在登录时检查密码,请执行以下操作
QuerySnapshot query = await FirebaseFirestore.instance.collection('users').where('email',isEqualTo:email).get();//You can also pass this query as a parameter when you push the login screen to save some time
if (query.docs[0].get('pass')==pass){
//Go ahead login
}
else{
//Display an error message that password is wrong
}
Run Code Online (Sandbox Code Playgroud)
如果您想存储数据以便在用户下次打开应用程序时自动登录,请查看共享首选项库。
如果您想传递用户数据,请创建一个名为 User() 的类
class User {
String email;
String pass;
String nick;
User(this.email,this.nick,this.pass);
}
Run Code Online (Sandbox Code Playgroud)
然后您可以传递它并轻松管理用户数据
| 归档时间: |
|
| 查看次数: |
12951 次 |
| 最近记录: |