我一直在尝试创建一个登录屏幕;如果用户使用其电子邮件和密码登录,则将转到用户屏幕;但是,如果管理员使用其管理员电子邮件和密码登录,则该应用会将管理员发送到管理员页面。我创建了一种方法,如果用户在Firestore上没有任何集合,则应用程序将提供一种形式来写入其信息;如果他们在Firestore上有一个收藏,则转到用户屏幕。 代码如下:
用于身份验证的对象
'最终的FirebaseAuth _auth = FirebaseAuth.instance;'
使用电子邮件和密码登录
//sign-in with e-mail and password
Future signInEmail (String email, String password) async{
try{
AuthResult result = await _auth
.signInWithEmailAndPassword(email: email, password: password);
FirebaseUser user = result.user;
return _userFromFirebaseUser(user);
} catch(e){
print(e.toString());
return null;
}
}
在输入页面中使用电子邮件和密码登录
//... log-in page UI and form for taking e-mail and password from users
//...
//log-in via e-mail
void signIn() async {
if (_formKey.currentState.validate()) {
var firebaseUser = await FirebaseAuth.instance.currentUser();
dynamic result = await _auth.signInEmail(email, password);
if (result == null) { // if email is false
setState(() {
loading = false;
error = "Wrong email";
});
} else if(firebaseUser.uid == "aMDsuSJ9h6eIJuWX0SvwmXJTvTJ3"){ // tried to find admin with its uid
Navigator.push(context, MaterialPageRoute(builder: (context) => AdminHome()));
} else { // sends to account page
var firebaseUser = await FirebaseAuth.instance.currentUser();
final snapShot = await Firestore.instance.collection("membership")
.document(firebaseUser.uid)
.get();
if(snapShot.exists){ // if user has a collection, go to user screens
Navigator.push(context, MaterialPageRoute(builder: (context) => AccountHome()));
} else{ // if user doesn't have any collection, pop-up a dialog
userInfoDialog(context);
}
}
}
}
答案 0 :(得分:1)
您将在firebaseUser
之前获得await _auth.signInEmail
参考。实际上,我希望firebaseUser
是null
。您确定在firebaseUser.uid
上没有收到例外情况吗?
您应该替换这两行,如:
dynamic result = await _auth.signInEmail(email, password);
var firebaseUser = await FirebaseAuth.instance.currentUser();
看看是否可行。