Chris
Chris

Reputation: 2034

How to: Set Firebase user's profile info on signup/creation? (Flutter-web)

How can I set a user's displyName at the same time as creating their account?

I'm using the createUserWithEmailAndPassword method and trying to take the information from 3 different TextFormFields within the same Form.

Below is a very simple example of what I'm trying to do... Hopefully from this someone will be able to help..

Thanks

This is my signup method:

import 'package:firebase_auth/firebase_auth.dart';

class AuthService {
  FirebaseAuth auth = FirebaseAuth.instance;

  //Create user with email and password (+ displayName)
  signUp({String email, String password, String name}) async {
    await FirebaseAuth.instance
        .createUserWithEmailAndPassword(email: email, password: password);


        // I'd like to create/update the new user's displayName here, using the String value (name) being passed into this function.



  }
}

This is an example of where the data is coming from:

class SignUpForm extends StatelessWidget {
  final GlobalKey<FormState> _formKey = GlobalKey();

  String name;
  String email;
  String password;

  TextEditingController nameController;
  TextEditingController emailController;
  TextEditingController passwordController;

  submit(){
    AuthService().signUp(password: 'password', email: email, name: name);
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Column(
        children: <Widget>[
          TextFormField(
            controller: nameController,
            onChanged: (value) {
              name = value;
            },
          ),
          TextFormField(
            controller: emailController,
            onChanged: (value) {
              email = value;
            },
          ),
          TextFormField(
            controller: passwordController,
            onChanged: (value) {
              password = value;
            },
          ),
        ],
      ),
    );
  }
}

Upvotes: 1

Views: 3325

Answers (2)

matwr
matwr

Reputation: 1571

The user object is returned from the createUserWithEmailAndPassword function's promise and you can then update the displayName immediately after by issuing a further request to firebase.

 await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password)
          .then((user){
    var userUpdateInfo = new UserUpdateInfo(); //create user update object
    userUpdateInfo.displayName = "John Doe"
    await firebaseAuth.updateProfile(userUpdateInfo); //update to firebase
    await user.reload(); //reload  user data
})

More info about UserUpdateInfo class here: https://pub.dev/documentation/firebase_auth/latest/firebase_auth/UserUpdateInfo-class.html

You may also want to check out the example app on the firebase github repository. I've linked to the file and line which is relevant to what you are seeking to achieve:

https://github.com/FirebaseExtended/flutterfire/blob/7ccdd3b9bca948d15b397fe5c86ec4616b611c47/packages/firebase_auth/firebase_auth/example/lib/register_page.dart#L88

EDIT

Final working code:

class AuthService {
FirebaseAuth auth = FirebaseAuth.instance;

signUp({String email, String password, String name}) async {
await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password)
.then(
(value) async {
var userUpdateInfo = new UserUpdateInfo(); //create user update object
userUpdateInfo.displayName = "John Doe";
await value.user.updateProfile(userUpdateInfo); //update to firebase
await value.user.reload();

print('displayname= ${userUpdateInfo.displayName}');
},
);
}
} 

Upvotes: 5

Henok
Henok

Reputation: 3383

_createUser() async {
await _auth
    .createUserWithEmailAndPassword(
  email: emailText,
  password: passwordText,
)
FirebaseUser user = await _auth.currentUser();

  UserUpdateInfo updateInfo = UserUpdateInfo();
  updateInfo.displayName = 'John Doe';
  await user.updateProfile(updateInfo);
  print('USERNAME IS: ${user.displayName}');
}

Upvotes: 0

Related Questions