简体   繁体   English

Flutter | Firebase 用户身份验证和配置文件创建

[英]Flutter | Firebase User Authentication & Profile creation

I am new to Flutter/Firebase and I want to program an app where a user can login/register and then he needs to create a profile with his information like his name, age... and only if he has created his profile, he should be able to continue and see the "main part" of the app.我是 Flutter/Firebase 的新手,我想编写一个用户可以登录/注册的应用程序,然后他需要使用他的姓名、年龄等信息创建个人资料......并且只有当他创建了个人资料时,他应该能够继续并看到应用程序的“主要部分”。

I already implemented the Firebase Auth with a working Login / Register Page, but my question is now, how to create the Profile thing the most efficent.我已经使用有效的登录/注册页面实现了 Firebase 身份验证,但我现在的问题是,如何最有效地创建配置文件。

At the moment I created this method here at my own:目前我在这里自己创建了这个方法:

  Future checkUserProfile() async{

// get snapshot from document
final snapShot = await Firestore.instance.collection('profiles').document(uid).get();

if(snapShot == null || !snapShot.exists){

  User.gotProfile = false;

} else {

  User.gotProfile = true;

}

This method is checking if an user-profile with the Firebase Auth User UID already exists and if not, the user will be send to the "ProfilePage" with a FutureBuilder executing the method above and if it already exists, he will see the main part of the app.此方法正在检查具有 Firebase Auth 用户 UID 的用户配置文件是否已经存在,如果不存在,用户将被发送到执行上述方法的 FutureBuilder 的“ProfilePage”,如果它已经存在,他将看到主要部分的应用程序。

As I already said, I tried it by myself and I wanted to ask if this is already an good implementation or is there even an easier & better way to do it?正如我已经说过的,我自己尝试过,我想问一下这是否已经是一个很好的实现,或者是否有更简单和更好的方法来做到这一点?

Yes this is an good implementation.是的,这是一个很好的实现。 In my app I have the check User method like yours.在我的应用程序中,我有像您一样的检查用户方法。 The following method is an example.下面的方法是一个例子。 When the user is not registered he forwarded to the RegisterPage else he forwarded to the MainPage.当用户未注册时,他转发到 RegisterPage,否则他转发到 MainPage。

checkUserAlreadyExists(FirebaseUser user) async {
    final userData = await Firestore.instance.collection('users').document(user.uid).get();
    if (userData == null || !userData.exists) {
      setState(() {
        Navigator.pushAndRemoveUntil(context,
            MaterialPageRoute(builder: (BuildContext context) => RegisterPage()), ModalRoute.withName('/'));
      });
    } else {
      setState(() {
        Navigator.pushAndRemoveUntil(context,
            MaterialPageRoute(builder: (BuildContext context) => MainPage()), ModalRoute.withName('/'));
      });
    }
  }

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

相关问题 Firebase身份验证获取用户配置文件 - Firebase Authentication Get User Profile 如何:在注册/创建时设置 Firebase 用户的个人资料信息? (颤振网) - How to: Set Firebase user's profile info on signup/creation? (Flutter-web) Firebase 未经许可更新用户配置文件的身份验证 - Firebase Authentication updating user profile without permission 上传用户头像 Firebase - Flutter - Upload user profile picture Firebase - Flutter Flutter 中的 Firebase 身份验证检查用户是否是新用户 - Firebase Authentication in Flutter Check if user is a new user 将电话号码存储在用户信息/用户配置文件中,无需在 Firebase 中进行身份验证 - Storing phone number in user info/user profile without authentication in Firebase 在 Flutter 中使用 Firebase 身份验证检查用户是否是新用户 - Check if user is new using Firebase Authentication in Flutter 如何使用 Firebase 身份验证在 Flutter 中注销用户 - How to Signout a user in Flutter with Firebase authentication Flutter:Firebase 身份验证无需登录即可创建用户 - Flutter: Firebase authentication create user without logging In 使用 Firebase 身份验证在 Flutter 中注销用户 - signOut of user in Flutter using Firebase authentication is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM