最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - firebase add user information while creating user (react.js) - Stack Overflow

programmeradmin1浏览0评论

I researched before asking this question but I couldn't find an answer.

I made a form in react js. When I click the button it creates a user with createUserWithEmailAndPassword() method. There is no problem with creating the user. I want to add extra information like; username : "Usernameeeee" to my database. The database will look like this:

users {
     userUid1: {name: "Name User 1"}
     userUid2: {name: "Name User 2"}
     ....
       }

When I use createUserWithEmailAndPassword(), it creates a user and logs them in. But as you can see the above, I need the current user's id. I know how to get it normally, firebase.auth().currenUser... But while creating a user I can't catch the user's id. It returns null;

export function signUpUser({email, password}) {
return function (dispatch){
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(
    () => dispatch({type: USER_SIGNUP_SUCCESS})
)
.then(console.log(firebase.auth().currentUser))
.catch(error => {
    dispatch({type: USER_SIGNUP_FAIL, payload: error.message});
});
}

}

When I console the current user, the creating user is plete but not logged in yet. So how can I add information while creating a user ? (not after logged in some other way)

I researched before asking this question but I couldn't find an answer.

I made a form in react js. When I click the button it creates a user with createUserWithEmailAndPassword() method. There is no problem with creating the user. I want to add extra information like; username : "Usernameeeee" to my database. The database will look like this:

users {
     userUid1: {name: "Name User 1"}
     userUid2: {name: "Name User 2"}
     ....
       }

When I use createUserWithEmailAndPassword(), it creates a user and logs them in. But as you can see the above, I need the current user's id. I know how to get it normally, firebase.auth().currenUser... But while creating a user I can't catch the user's id. It returns null;

export function signUpUser({email, password}) {
return function (dispatch){
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(
    () => dispatch({type: USER_SIGNUP_SUCCESS})
)
.then(console.log(firebase.auth().currentUser))
.catch(error => {
    dispatch({type: USER_SIGNUP_FAIL, payload: error.message});
});
}

}

When I console the current user, the creating user is plete but not logged in yet. So how can I add information while creating a user ? (not after logged in some other way)

Share Improve this question edited Jul 11, 2017 at 16:11 Ben Hare 4,4255 gold badges29 silver badges46 bronze badges asked Jul 11, 2017 at 15:57 escalepionescalepion 6812 gold badges7 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

I found the answer after a day. I hope this will be helpful to others. As you understand, the main problem is getting the user id right after creating a user, before user sign in...

So the solution is:

export function signUpUser({email, password, username}) {
    const db = firebase.database();
    return function (dispatch){
    firebase.auth().createUserWithEmailAndPassword(email, password)
      // here we get user id if creating is successful.
    .then(function(user){
        dispatch({type: USER_SIGNUP_SUCCESS}); // I dispatched some message.
        db.ref(`users/${user.uid}`).set({name: username}); // I added user
        console.log('uid:',user.uid)

})
发布评论

评论列表(0)

  1. 暂无评论