Advertisement Jump to content
Sign in to follow this  
CelticSir

NodeJS sessions - i'm really confused

This topic is 1864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm really confused with how to check a user is who they say they are in a chat room by checking against the mySQL database.

 

First the user logs in, and an ajax request is sent to a PHP file which checks the login info and sets a session like:
 

$_SESSION['uid'] = $row['uid'];

Now in my nodeJS  in app.js i want to check this session so i know they are logged in and i have their userID so i know who is sending the message.

 

The idea is like this using Express/SocketIO & mysql-node:
 

socket.on('sendMessage', function(data,callBack){ 
var userID = //assign $_SESSION['uid'], possible?

if(!userID){ 
   console.log('User not logged in!');
} else {

var message = sanitize(data['message']).escape();
  
  var query = connection.query('SELECT name FROM users WHERE uid = ?', [userID],

    function(err,results){
      if(err){
       console.log('Query Error: '+err);
      } else if(results.length == 1){
        var username = results[0].name;
        console.log(username+' sent a message!');
      }
  });

});

Is this possible this way? If not - how do you actually do it ?

Edited by thefollower

Share this post


Link to post
Share on other sites
Advertisement

Why don't you just print out the session variable, like with 'console.log' or whichever other command you have available to you. At least then you'll know what data you can use to verify the user (and if it is available at all in the session variable).

 

You should also sanitize the userID before injecting it into your SQL statement.

Edited by RH101

Share this post


Link to post
Share on other sites

$_SESSION['uid'] is not going to available to me in the app.js automatically, i need some way for it to get it from PHP, given they are different languages.

 

The problem is i don't know how to get $_SESSION['uid'] to in app.js

 

Also yeah i will sanitize once i know how to assign the session :P

Edited by thefollower

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!