在Flutter中使用Firebase身份验证检查用户是否是新用户 [英] Check if user is new using Firebase Authentication in Flutter
问题描述
根据此页面底部,AdditionalUserInfo提供了一种称为isNewUser()
的方法,用于检查例如社交登录名(Facebook,Google等)是登录还是注册. 此答案. Flutter的问题是我找不到任何名为AdditionalUserInfo
的类或函数称为getAdditionalUserInfo()
.有谁知道如何检查用户是否首次注册,最好是在Facebook& Firebase身份验证.
According to the bottom of this page, AdditionalUserInfo provides a method called isNewUser()
to check for example if a social login (Facebook, Google etc.) is a sign in or a sign up. An example is given in this answer. The problem with Flutter is that I cannot find any class called AdditionalUserInfo
or a function called getAdditionalUserInfo()
. So does anyone know how to check if a user registered for the first time, preferably with Facebook & Firebase Authentication.
我正在使用 firebase_auth 和推荐答案
AdditionalUserInfo是 AdditionalUserInfo is a method of the 由于该问题是针对Facebook登录的,所以我也添加了该代码.下面的代码使用 Since the question was meant for Facebook Login I am adding that code as well. The code below uses 这篇关于在Flutter中使用Firebase身份验证检查用户是否是新用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!AuthResult
类的方法.如果要检查用户是否是首次登录,则可以使用additionalUserInfo.isNewUser
的值.以下代码使用Google Auth登录用户.AuthResult
class. If you want to check if the user is logging in for the first time then you can use the value of additionalUserInfo.isNewUser
. The following code logs in a user using Google Auth.Future<FirebaseUser> _signIn() async {
try {
GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
GoogleSignInAuthentication gSA = await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: gSA.accessToken,
idToken: gSA.idToken,
);
AuthResult authResult = await _auth.signInWithCredential(credential);
if (authResult.additionalUserInfo.isNewUser) {
//User logging in for the first time
// Redirect user to tutorial
}
else {
//User has already logged in before.
//Show user profile
}
} catch (e) {
print(e.message);
}}
flutter_facebook_login
和firebase_auth
软件包.flutter_facebook_login
and firebase_auth
packages.Future<FirebaseUser> _facebookAuthHandler() async {
final result = await facebookLogin.logInWithReadPermissions(['email']);
final AuthCredential credential = FacebookAuthProvider.getCredential(accessToken: result.accessToken.token);
AuthResult authResult = await _auth.signInWithCredential(credential);
if (authResult.additionalUserInfo.isNewUser) {
//TODO User logging in for the first time. Redirect to Pages.SignUp Page
}
else {
//TODO User has already logged in before. Redirect to Home Page
}
}