Flutter user registration with Firebase: Add extra user information (age, username)

无人久伴 提交于 2020-05-14 14:23:36

问题


With a phpMySQL background, I'm a little bit lost.

I've successfully created a registration process with Flutter and Firebase Auth (simple email/password method) following a tutorial.

I would like add a "username" and "age" field to the registration form.

In Firebase I've created a database called "users", and in it a String typed "userid" that's blank. But is it even necessary? What do I do next? How is it mapped to the User UID in the authentication table? How do I push it and retrieve it with Flutter?

I've explored this post to no avail Add extra User Information with firebase

If that helps, my authentication file contains this:

class Auth implements BaseAuth {
  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

  Future<String> signIn(String email, String password) async {
    FirebaseUser user = await _firebaseAuth.signInWithEmailAndPassword(
        email: email, password: password);
    return user.uid;
  }

  Future<String> signUp(String email, String password) async {
    FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(
        email: email, password: password);
    return user.uid;
  }

Have tried this:

Future<String> signUp(String email, String password) async {
    FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(
        email: email, password: password);

    Firestore.instance.collection('users').document().setData({ 'userid': user.uid, 'displayName': 'bobby' });

    return user.uid;
  }

But it throws an error:

5.17.0 - [Firebase/Firestore][I-FST000001] Write at users/-L_6e1CFkU1YchxsSPay failed: Missing or insufficient permissions.

回答1:


Ok seems the above is all good. Just had to change the Database rules from false to true as per the below post:

https://stackoverflow.com/a/46925637/3475894



来源:https://stackoverflow.com/questions/54977160/flutter-user-registration-with-firebase-add-extra-user-information-age-userna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!