[英]How to detect firebase user registration and signIn successfully in flutter
As per the documentation of FlutterFire, there are two different methods for signIn and signUp for email and password authentication.根据 FlutterFire 的文档,有两种不同的登录和注册方法用于电子邮件和密码身份验证。
For signUp注册
try {
UserCredential userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: "barry.allen@example.com",
password: "SuperSecretPassword!"
);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');
}
} catch (e) {
print(e);
}
For signIn用于登录
try {
UserCredential userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: "barry.allen@example.com",
password: "SuperSecretPassword!"
);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('No user found for that email.');
} else if (e.code == 'wrong-password') {
print('Wrong password provided for that user.');
}
}
By this code I can handle error by FirebaseAuthException
and stop some loading progress.通过这段代码,我可以通过FirebaseAuthException
处理错误并停止一些加载进度。 But how do I detect that user successfully register and do something as per success registration?但是如何检测该用户成功注册并按照成功注册执行某些操作?
But how do I detect that user successfully register?但是如何检测该用户成功注册?
It is actually detailed in the documentation section that explains the code you mention in your question.它实际上在解释您在问题中提到的代码的文档部分中有详细说明。
In particular, for registration the doc says:特别是,对于注册,文档说:
The method is a two-step operation ;该方法是一个两步操作; it will first create the new account (if it does not already exist and the password is valid) and then automatically sign in the user in to that account .它将首先创建新帐户(如果它不存在且密码有效),然后自动将用户登录到该帐户。 If you are listening to changes in authentication state, a new event will be sent to your listeners .如果您正在侦听身份验证状态的更改,则会向您的侦听器发送一个新事件。
To listen to changes in authentication state you can call the authStateChanges()
method on your FirebaseAuth
instance, as follows:要监听身份验证状态的变化,您可以调用FirebaseAuth
实例上的authStateChanges()
方法,如下所示:
FirebaseAuth.instance
.authStateChanges()
.listen((User? user) {
if (user == null) {
print('User is currently signed out!');
} else {
// !!!!! Here you know the user is signed-in !!!!!
print('User is signed in!');
}
});
For signIn, it is the same.对于登录,也是一样的。 The doc states that:该文档指出:
Once successful, if you are listening to changes in authentication state, a new event will be sent to your listeners.成功后,如果您正在侦听身份验证状态的更改,则会向您的侦听器发送一个新事件。
Refer to the registration case above.参考上面的注册案例。
So, in summary, on one hand you call the methods to either register ( createUserWithEmailAndPassword()
) or sign-in ( signInWithEmailAndPassword()
) and, on the other hand, you set a listener that listens to changes in authentication state.因此,总而言之,一方面您调用方法来注册( createUserWithEmailAndPassword()
)或登录( signInWithEmailAndPassword()
),另一方面,您设置一个侦听器来侦听身份验证状态的变化。 The listener alerts you when the call to one of the two methods is successful.当对两种方法之一的调用成功时,侦听器会提醒您。
The on FirebaseAuthException catch (e) {...}
blocks deals with the cases when the call to one of the two methods is not successful. on FirebaseAuthException catch (e) {...}
块处理对两种方法之一的调用不成功的情况。
Finally, note the following article from the Firebase Developers Medium publication: it presents a slightly different approach, declaring async
methods which check if the user
object returned by the methods is null
or not, and call setState()
accordingly.最后,请注意Firebase Developers Medium 出版物中的以下文章:它提出了一种稍微不同的方法,声明了async
方法,用于检查方法返回的user
对象是否为null
,并相应地调用setState()
。
For example:例如:
void _register() async {
final FirebaseUser user = (await
_auth.createUserWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
)
).user;
if (user != null) {
setState(() {
_success = true;
_userEmail = user.email;
});
} else {
setState(() {
_success = true;
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.