Reputation:
I'm building an app with firebase and I want to save the user name to display it later in the profile Page. I tried some things but I did not know how to do it in the right way because I'm a beginner in flutter.
firebaseHelper.dart
Future<AuthResultStatus> createAccount({email, password}) async {
try {
AuthResult authResult = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
if (authResult.user != null) {
_status = AuthResultStatus.successful;
} else {
_status = AuthResultStatus.undefined;
}
} catch (e) {
print('Exception @createAccount: $e');
_status = AuthExceptionHandler.handleException(e);
}
return _status;}
signup.dart
createAccount() async {
final status = await FirebaseHelper().createAccount(
email: emailInputController.text,
password: pwdInputController.text);
if (status == AuthResultStatus.successful) {
// Navigate to success page
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => MyNavHomePage()),
(r) => false);
} else {
final errorMsg = AuthExceptionHandler.generateExceptionMessage(status);
_showAlertDialog(errorMsg);
}}
If I miss something by accident please tell me. Thank you in advance.
Upvotes: 1
Views: 1726
Reputation: 560
You have to store the data in firestore.
When you create the user's account, create a new user doc in a collection of users. Make the name of the doc the uid of the new user you created with auth. In that document you can store the name and the rest of the data you want from that user.
When you want to get the name of the logged-in user, get the doc named with the uid of the user.
Firebase authentication is just for login
To store user data -> firestore
Example:
final db = Firestore.instance;
FirebaseAuth auth = FirebaseAuth.instance;
//This method is used to create the user in firestore
Future<void> createUser(String uid, String username, String email, int age) async {
//Creates the user doc named whatever the user uid is in te collection "users"
//and adds the user data
await db.collection("users").document(uid).setData({
'Name': username,
'Email': email,
'Age' : age
});
}
//This function registers a new user with auth and then calls the function createUser
Future<void> registerUser(String email, String password, String username) async {
//Create the user with auth
AuthResult result = await auth.createUserWithEmailAndPassword(email: email, password: password);
//Create the user in firestore with the user data
createUser(result.user.uid, username, email, age);
}
//Function for logging in a user
Future<void> logIn(String email, String password) async {
//sign in the user with auth and get the user auth info back
FirebaseUser user = (await auth.signInWithEmailAndPassword(email: email, password: password)).user;
//Get the user doc with the uid of the user that just logged in
DocumentReference ref = await db.collection("users").document(user.uid);
DocumentSnapshot snapshot = await ref.get()
//Print the user's name or do whatever you want to do with it
print(snapshot.data["Name"]);
Upvotes: 1