简体   繁体   English

在 Flutter 中使用 Firebase 身份验证检查用户是否是新用户

[英]Check if user is new using Firebase Authentication in Flutter

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.根据此页面的底部,AdditionalUserInfo 提供了一个名为isNewUser()的方法来检查例如社交登录(Facebook、Google 等)是登录还是注册。 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() . Flutter 的问题是我找不到任何名为AdditionalUserInfo类或名为getAdditionalUserInfo()的函数。 So does anyone know how to check if a user registered for the first time, preferably with Facebook & Firebase Authentication.有谁知道如何检查用户是否是第一次注册,最好使用 Facebook 和 Firebase 身份验证。

I am using firebase_auth and flutter_facebook_login .我使用firebase_authflutter_facebook_login

AdditionalUserInfo is a method of the AuthResult class. AdditionalUserInfo 是AuthResult类的一个方法。 If you want to check if the user is logging in for the first time then you can use the value of additionalUserInfo.isNewUser .如果要检查用户是否是第一次登录,则可以使用additionalUserInfo.isNewUser的值。 The following code logs in a user using Google Auth.以下代码使用 Google 身份验证登录用户。

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);
}}

Edit: Since the question was meant for Facebook Login I am adding that code as well.编辑:由于该问题是针对 Facebook 登录的,因此我也添加了该代码。 The code below uses flutter_facebook_login and firebase_auth packages.下面使用代码flutter_facebook_loginfirebase_auth包。

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
  }

}

I do not think this is implemented in the current version of the package. 我不认为这是在当前版本的软件包中实现的。 I needed something similar sometime ago. 我前段时间需要类似的东西。 What I had to do is to add to the user document a flag of isNew:true from my cloud function and change it for each user upon passing some actions in my app, for example the tutorial phase. 我必须做的是从我的云函数向用户文档添加isNew:true的标志,并在我的应用程序中传递一些操作时为每个用户更改它,例如教程阶段。

In the recent version, there's no authResult class, instead there's userCredential class.在最近的版本中,没有 authResult 类,而是有 userCredential 类。 Which will give you the additional information about the user and so you will know whether the user is knew.这将为您提供有关用户的其他信息,因此您将知道该用户是否被认识。

Future<void> signInWithGoogle() async {
GoogleSignInAccount account = await googleSignIn.signIn();
GoogleSignInAuthentication authentication = await account.authentication;
OAuthCredential credential = GoogleAuthProvider.credential(
  accessToken: authentication.accessToken,
  idToken: authentication.idToken,
);
UserCredential userCredential =
    await _firebaseAuth.signInWithCredential(credential);
User user = userCredential.user;
if (userCredential.additionalUserInfo.isNewUser) {
  //Here, you can do something with the profile of a new user. eg. upload information  
  //in database
}

} }

Or just do something like that:或者只是做这样的事情:

final userCredential = await _firebaseAuth.signInWithPopup(provider);
final username = userCredential.additionalUserInfo?.username;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 中的 Firebase 身份验证检查用户是否是新用户 - Firebase Authentication in Flutter Check if user is a new user 使用 Firebase 身份验证在 Flutter 中注销用户 - signOut of user in Flutter using Firebase authentication is not working 在 Flutter 中使用 FirebaseAuth 检查用户的身份验证状态 - check authentication state of User using FirebaseAuth in Flutter 如何使用 flutter 检查用户是否已存在于 firebase 身份验证中? - How do I check if a user is already exist in firebase Authentication using flutter? 我是 flutter web 的新手,如何使用 firebase 电话身份验证对用户进行身份验证,有没有办法让用户保持登录状态? - I am new to flutter web, how to authenticate users using firebase phone authentication, is there a way to keep user logged in? Flutter:检查OTP认证后是现有用户还是新用户 - Flutter: Check if existing user or new user after OTP Authentication 有什么方法可以检查用户是否是 flutter firebase 的新用户(Facebook 用户)? - Is there any way to check if a user is new in flutter firebase (A facebook user)? 在flutter中使用firebase登录google时如何检查用户是新用户还是现有用户? - How to check If user is new or existing when login with google using firebase in flutter? 如何使用 flutter 检查电话号码是否已在 firebase 身份验证中注册 - How to check if phone number is already registered in firebase authentication using flutter 检查用户是否是firebase auth中的新用户的最佳方法是什么? - What is the best way to check if the user is new in firebase auth with flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM