在Flutter中使用Firebase身份验证检查用户是否是新用户 [英] Check if user is new using Firebase Authentication in Flutter

查看:177
本文介绍了在Flutter中使用Firebase身份验证检查用户是否是新用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此页面底部,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是AuthResult类的方法.如果要检查用户是否是首次登录,则可以使用additionalUserInfo.isNewUser的值.以下代码使用Google Auth登录用户.

AdditionalUserInfo is a method of the 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);
}}

由于该问题是针对Facebook登录的,所以我也添加了该代码.下面的代码使用flutter_facebook_loginfirebase_auth软件包.

Since the question was meant for Facebook Login I am adding that code as well. The code below uses 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
  }

}

这篇关于在Flutter中使用Firebase身份验证检查用户是否是新用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆