I am authenticating users through Firebase Authentication and saving some extra metadata about user in users collection. I want to perform create operation the way that either both can get created or failed. I don't want to create at only one place.
I have below code so far. It is very dangerous because it is highly possible that only one can get created and one can get failed:
const newUser = req.body;
try {
const user = await admin.auth().createUser(newUser);
/* Add user to users collection */
usersRef.doc(user.uid).set({
type: 'user',
notification: '',
bio: ''
});
res.status(201).json(response(user.uid, ''));
} catch (err) {
res.status(400).json(response(null, 'Something went wrong! Please try again.'));
}
I am fairly new to Firebase. I've very limited knowledge on the subject. I would really appreciate if anyone can redirect me to right direction. Thanks in advance!