This question already has answers here:
Adding new data to firebase users
(2 answers)
Closed 2 years ago.
Currently I have small project using react native and I am using Firebase as backend. I have registration form with more than two fields (name, phone num, gender, etc), but with Firebase we can only register a user with email and password. So, my question is how I can save other information of user and later how I would retrieve that information?
On your backend you can create 'users' collection and save it after registering a user
const data = {
email: 'email',
password: 'password'
}
firebase.auth().createUser(data)
.then((userRecord) => {
var uid = userRecord.uid;
return firebase.firestore().collection('users').doc(uid)
.set({
user_uid: uid,
gender: '',
...
})
})
and later you can use doc id to retrieve extra information
firebase.firestore().collection('users').doc(uid).get()
Related
I'm creating a data base of user accounts with embedded profile documents so I can store the data separately but in a way where they are still linked. I want to include a the ID of the user account within the profile document in order to reference the user account in my app without passing the user account info to the client (eg email and password).
The problem is that for the user ID to be generated the user document needs to be created, but if I create the user document first then I cant link the profile to it as it has not yet been created. If I try to tackle this from the other way round then I cant add the userID to the profile as the ID will not yet have been created...
const newProfile = new Profile ({
companyName: companyName,
eventGenre: eventGenre,
area: area,
established: established,
description: pDescription,
userID: //(this should be the ID of the user docuemnt created below)
})
newProfile.save()
const newUser = new User ({
email: email,
password: password,
remeberMe: 0,
accountType: 1,
promoterProfile: newProfile,
venueProfile: null
})
newUser.save(function(err){
if(err){
console.log(err);
}
This question already has an answer here:
How to get FirebaseUser from a uid?
(1 answer)
Closed 8 months ago.
New to vue/firebase. I was able to lookup how to pull up “current user” data from auth no problem but trying to write a js composable that I can pass in any user id (not necessarily current user) and it will return the user object or at least displayName.
All the docs/vids I can find on the topic reference getting info on *current user *only not another user. From the Google Docs it says I should be able to do this in the "Retrieve user data" section. Closest model to Vue code-wise seems to be “Node.Js” but it isn't working.
Here's what I've got in getUserById
import { getAuth } from 'firebase/auth'
const getUserById = (u) => { // u = user id
const userData = null
getAuth()
.getUser(u)
.then((userRecord) => {
// See the UserRecord reference doc for the contents of userRecord.
console.log(`Successfully fetched user data: ${userRecord.toJSON()}`);
userData = userRecord
})
.catch((error) => {
console.log('Error fetching user data:', error);
});
return { userData }
}
export default getUserById
The error I get is getUser is not a function. I tried adding getUser to the import but same error.
There is no way to look up information about just any user by their UID in the client-side APIs of Firebase as that would be a security risk. There is an API to look up a user by their UID in the Admin SDK, but that can only be used on a trusted environment (such as your development machine, a server you control, or Cloud Functions/Cloud Run), and not in client-side code.
If you need such functionality in your app, you can either wrap the functionality from the Admin SDK in a custom endpoint that you then secure, or have each user write information to a cloud database (such as Realtime Database or Firestore) and read it from there.
Also see:
How to get FirebaseUser from a uid?
Firebase get user by ID
Is there any way to get Firebase Auth User UID?
This question already has an answer here:
change displayName of the currentUser in firebase [closed]
(1 answer)
Closed 2 years ago.
I want to update displayName of the current user and display it on screen.
firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then((response) => {response.user.updateProfile({displayName: 'John Doe'}),response.user.reload()})
.catch(error => Alert.alert('', error.message));
console.log(firebase.auth().currentUser.displayName);
using this code it is returning null on first but when I reload the app it is returning 'John Doe' how can i do it without reloading app.
Call reload() on the user object after updating the profile. Note that it returns a promise to indicate when that's done.
I am trying to add facebook login in my app. And its successfuly working but problem is every time i login with facebook it change the firestore data. Here is my code
onLoginSuccess(res: FacebookLoginResponse) {
const credential = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
this.fireAuth.auth.signInWithCredential(credential)
.then((response) => {
console.log(response);
this.fb.api('me?fields=id,name,email,first_name,picture.width(720).height(720).as(picture_large)', []).then(profile => {
this.userData = {email: profile['email'], first_name: profile['first_name'], picture: profile['picture_large']['data']['url'], username: profile['name']}
console.log(this.userData);
console.log(this.fireAuth.auth.currentUser.uid);
let userData = {
email: this.userData.email,
username: this.userData.username,
photo: this.userData.picture,
phone: '',
point : '0',
uID: this.fireAuth.auth.currentUser.uid
};
this.angularFirestore.collection('AppUsers').doc(this.fireAuth.auth.currentUser.uid).set(userData); //Here is the problem
this.router.navigate(["/select"]);
})
});
}
I need to check if there is doc already available with this.fireAuth.auth.currentUser.uid so it will not add in database. IF not avaiable it will store in database.
So i need the method how can i check docID exist or not ?
There are some ways that you can get the information if a document exists on Firestore or not.
On this question of the Community here, there is all the logic for how to create a function to check a single document on the Firestore database - it's in Java, but I believe the logic should help you.
On this other post How can i get single document from Firestore with query (I'm using Ionic 4 ), there is a query to return a single document as well, in case you think this one fits you better.
Let me know if the information helped you!
If your problem is "every time i login with facebook it change the firestore data" you can use "update" instead of "set".
Here is the docs about it: Firestore Update Doc
I'm using firebase web authentication to log users into my website which works perfectly fine - but I want to be able to show the name of the user logged in. How do I do that?
When a user signs up, it will be written with some basic fields (email, uniqueID, etc) to the Firebase Authentication DB (which you have access to, but you don't have full control of, since it's controlled by the Firebase Auth system).
Once your brand new user is created, you'll get a UserCredential object with basic information of your user. Use the uniqueID field (UserCredential.user.uid) to write your user details with as many fields as you want (username, email, fullName, address, preferences, etc) to your Firebase database (realtime DB or cloud Firestore). I think it's a good practice to store under a document with the uniqueID created by the Auth system.
Once you've done that, and that users logins in at a later time, you'll get another userCredential object that contains the uniqueID for that user.
You'll use that uniqueID to access your database where you stored your users details and you will be able to retrieve all the data that you want.
See the code below (I'm using React): uppon a successful signUp, we get a UserCredential object, which we can use to read the uniqueId uid that it was just created, and use that to create a document in our database with that same id. We can add as many fields as we want. In this case, I added username (which I'm getting from my signup form) and email (which I'm getting directly from the UserCredential object).
UserCredential docs
Create User with Password and Email docs
handleSubmit(event){
event.preventDefault();
const { email, passwordOne} = this.state;
// SIGN UP FUNCTION
this.props.firebase.auth.createUserWithEmailAndPassword(email, passwordOne)
// SAVING USER DETAILS TO MY DATABASE
.then((UserCredential) => {
const username = this.state.username;
this.props.firebase.db.collection('users').doc(UserCredential.user.uid)
.set({
username: username,
email: UserCredential.user.email
})
.then( () => {
this.setState({ ...INITIAL_STATE });
this.props.history.push('/');
}
)
.catch(error => {
this.setState({ error });
}
);
}
)
.catch(error => {
this.setState({ error });
}
);
}