0

I know there are a few answers matching this question already. But I am looking for something else.

In the client side I have a signUp form that will send a post request to the server with username and password.

In the server side I authenticate the request. This is the code. (I use express)

server.post('/', function(req, res) {

 res.redirect('/');// I want to get rid of the "confirm resubmission message upon refreshing the page"
 authenticate(req.body.username){
        if(false){
           client.emit('signUpResponse',{success:false}); //cannot do it without a client's socket id
        } else {
        db.user.insert({username:req.body.username})
            client.emit('signUpResponse',{success:true}); //cannot do it without a client's socket id
        }
    };

At this stage I don't want to make another webpage(like/signup) to handle the signUp event.

I can access the client's socket id if I put the above code under the io.sockets.on code.

io.sockets.on('connection', function (client) {  

 server.post('/', function(req, res) {
 console.log(client.id) //now I can access the client's socket
 res.redirect('/');// I want to get rid of the "confirm resubmission message upon refreshing the page"
 authenticate(req.body.username){
        if(false){
           client.emit('signUpResponse',{success:false}); //nothing happens at the client' side. maybe something to do with the "res.redirect"?
        } else {
        db.user.insert({username:req.body.username})
            client.emit('signUpResponse',{success:true}); ////nothing happens at the client' side. maybe something to do with the "res.redirect"?
        }
    };

In the server.post handler, I dont want to use any method like res.send,res.redirect(newURL) as I want the client side to stay at the same page. So is there a way to communicate with client side in the "post" handler like client emit?

Frostless
  • 818
  • 1
  • 11
  • 33
  • `res.send` only sends a response to the caller. It does not redirect unless you do something more with it on the client side. At this point it sure sounds like you need to look up how to make an [Ajax call](http://stackoverflow.com/questions/9436534/ajax-tutorial-for-post-and-get) (Used to update data on a page without reloading). It's very likely you actually DO want to use `res.send`. Sorry if I interpreted that wrong. – ippi Apr 29 '17 at 08:45

1 Answers1

0

I take it that the site is using https. With that said you should just use socket.io. It's much more easier.

// On the client-side
var client = io();

client.on('userLoggedIn', function(confirm) {
  if (confirm) {
    // Do your post login confirmation stuff here
  }
});

var form = document.getElementById('login-form'),
  username = document.getElementById('username').value,
  password = document.getElementById('password').value;

form.addEventListener('submit', function(event) {
  client.emit('login', {
    username: username,
    password: password
  });

  // Stops the form from redirecting the web page to the action link
  event.preventDefault();
}, false);

// Server-side
io.on('connection', function(client) {
  // Login user
  client.on('login', function(user) {
    // Do your login stuff here
  });
});
Kitanga Nday
  • 3,517
  • 2
  • 17
  • 29
  • 1
    that is what a thought before, however I did not worked out how the prevent the form from submitting back then. Thanks. – Frostless Apr 29 '17 at 09:59
  • @WeiZheng Ah! I see, lol, well then it's the simple `event.preventDefault()` that does the trick. Use that whenever you don't want something to do what it usually does (e.g. cursor keys scrolling the page) – Kitanga Nday Apr 29 '17 at 10:11