I tried to save the docs from the collection "users" in Firebase with a phone number but to read these docs I need to call fonction these docs with number
model.User user = model.User(
uid: cred.user!.uid,
userName: userName,
portable: portable,
nni: nni,
email: email,
whatsapp: whatsapp,
photoUrl: photoUrl,
password: password,
);
await _firestore
.collection("users")
.doc(portable)
.set(user.toJson());
to read these data
Future<model.User> getUserDetails() async {
DocumentSnapshot snap =
await _firestore.collection('users').doc(portable).get();
return model.User.fromSnap(snap);
}
Related
I'm trying to get the uid, but it always returns the uid of the previously created account.
createUserWithEmailAndPassword does not execute the login after being completed.
So it should be returning the uid correctly, right?
Below are 2 pictures of the database and Authentication, as well as the code.
Future _singUp() async {
final User? user = auth.currentUser;
final uid = user!.uid;
try {
await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
);
await FirebaseFirestore.instance.collection('users').add({
'email': _emailController.text,
'name': _nameController.text,
'Id_usuario': uid
});
} on FirebaseAuthException catch (e) {
_handleSingUpError(e);
}
}
When you use createUserWithEmailAndPassword it will return a UserCredential as a result, you can use that to update your user variable, so in code it would be something like this:
UserCredential result = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password);
User? user = result.user;
In short, I want my Authenticated user by Email and Password in firebase firestore,
After successfully register my users only appears in a authentication pannel.
I am currently working with Flutter application where I use firebase_auth: ^1.1.1 package.
So I expect that after
UserCredential userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: "abc#example.com",
password: "SuperSecretPassword!"
);
The function called from firebase_auth package which is createUserWithEmailAndPassword
I want this registed user in Firebase Database.
Firebase authentication is used to register a user while firestore is used for storing data. If you want to add the user to firestore also then you need to add the cloud_firestore plugin to your pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
cloud_firestore: ^1.0.7
Then you can do:
final firestoreInstance = FirebaseFirestore.instance;
var firebaseUser = FirebaseAuth.instance.currentUser;
firestoreInstance.collection("users").doc(firebaseUser.uid).set(
{
"email" : "abc#example.com",
}).then((_){
print("success!");
});
This will create a collection called users with a document id equal to the authenticated user id, and inside of it you will have the email of the user.
.createUserWithEmailAndPassword creates a user in auth, if you check your auth tab you should see created user. It does not create user in firestore
for this you have to implement yourself .
below is a sample example
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
User user = (await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: password))
.user;
//if create user succeeds
var user =UserModel(
userID:user.uid,
email:user.email)
FirebaseFirestore.instance
//save user based on their id from auth
.doc("users/${user.uid}")
.set(user.toJson());
Edit
you can create a user model
example:
class UserModel {
final String userID;
final String displayName;
final String email, pushToken;
final String phoneNumber;
final String profilePictureURL, dateJoined;
UserModel({
this.dateJoined,
this.userID,
this.email,
});
Map<String, Object> toJson() {
return {
'userID': userID,
'email': email == null ? '' : email,
'appIdentifier': 'my app',
'dateJoined': DateTime.now(),
};
}
}
....
check how to use json
In addition to the griffins answer, you could use a cloud function to respond to the user creation event (trigger onCreated). Then you can create the users collection, asynchronously, without overloading the flutter client. For example, an index.ts with the createUserInFirestore function:
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
const db = admin.firestore();
export const createUserInFirestore = functions.auth.user().onCreate(
async (user) => {
return db.collection('users').doc(user.uid).set({
"email" : user.email,
"name" : user.displayName,
});
}
);
How to set firebase uid to users document id? i'm getting error as "uid is null". Here is my code.
FirebaseAuth.instance.createUserWithEmailAndPassword(email:email, password: password).then((currentUser) => Firestore.instance
.collection("users")
.document(currentUser.uid)
.setData({ "uid": currentUser.uid,
}));
You can become the current user information with the .user ending and then you have access to the uid with currentUser.uid.
Future<void> createUser() async {
final FirebaseUser currentUser = (await FirebaseAuth.instance.createUserWithEmailAndPassword(email: 'Test#gmail.com', password: '123456')).user;
Firestore.instance.collection("users").document(currentUser.uid).setData({
"uid": currentUser.uid,
},
);
}
I am trying to save the users who sign in in the firebase database.
this is the function which is used to update the signed in user in the firebase, the fuction uses a uid to create a document with this id :
final Firestore _db = Firestore.instance;
void upadteUserData(FirebaseUser user) async {
DocumentReference ref = _db.collection("users").document(user.uid);
print("in update");
return ref.setData({
"uid": user.uid,
'email': user.email,
'displayName': user.displayName,
//'emergency': []
}, merge: true);
}
and here is the sign in fuction:
Future signInWithEmailAndPassword(String email, String password) async {
try {
AuthResult result = await _auth.signInWithEmailAndPassword(
email: email, password: password);
FirebaseUser user = result.user;
upadteUserData(user);
print("signing in");
print(result.user.email);
return _userFromFirebaseUser(user);
} catch (e) {
print(e.toString());
return null;
}
}
here's the console after signing in
I tried it twice and it was working perfectly fine 3 weeks ago. However, when I try to sign in today with different e-mails, it did not update the fire base. Any idea?
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.
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