firebase : unable to user authorization data is undefined, but uid is retrieving correctly Node.js

517 views Asked by At

Actually, I want to globally use the data of the user., who logged into the portal currently. Below is the code i wrote.

I have saved the userdata to firebase database after creating the user as shown here!

firebase.auth().createUserWithEmailAndPassword(email,password).then(function(userData)
  {
    console.log("Successfully Created user with uid:",userData.uid);
      var user ={
        uid:userData.uid,
        email:email,
        first_name:first_name,
        last_name:last_name,
        location:location,
        fav_genres:fav_genres,
        fav_artists:fav_artists
      }
      var userRef =fbRef.child('users');
      userRef.push().set(user);
    req.flash('success_msg','you are registered now, You can login');
    res.redirect('/users/login');
  }).catch(function(error)
  {
    res.write
    ({
      code: error.code
    });
    res.status(401).end();
  });
}
});

Now i want to retrieve and use currenly logged in userdata globally for all the pages in my application.

some portion in app.js is below

app.get('*',function(req,res,next){
  if(firebase.auth().currentUser!=null)
  res.locals.user = firebase.auth().currentUser;
});

below is a node in users from firebase. enter image description here

But i am not getting the user object., Able to get only uid of the current user.Please suggest me in this.

1

There are 1 answers

0
Mahesh On BEST ANSWER

I could figure out the issue.Got it by using the orderByChild() function as shown below.

app.get('*',function(req,res,next){

  if(firebase.auth().currentUser!=null){
  var id = firebase.auth().currentUser.uid;
  var profileRef = fbRef.child('users');
  // order by uid and then find the specific uid
  var query = profileRef.orderByChild('uid').equalTo(id);
  // Do a one time read of that uid
  query.once('value', function(snap){
  var user = JSON.stringify(snap.val());
  console.log('User data is: '+user);
  res.locals.user = JSON.stringify(snap.val());
});
}
next();
});