Reputation: 25
In my application I am using socketio and nodejs. I am trying to implement simple authentication. If user is authorized I have to redirect to my application otherwise I have to redirect to same page. the sample code I implemented is
<script>
$(function(){
$('#join').click( function() {
var uname = $('#uname').val();
alert(uname);
socket.emit('news', uname);
var pwd = $('#pwd').val();
socket.emit('pass', pwd);
});
});
</script>
The server code is ..
app.get('/', function(req, res){
if(auth)
{
res.sendfile(__dirname + '/home.html');
}
else
{
res.send("hello user");
}
});
io.sockets.on('connection', function(socket){
var uname;
var pwd;
socket.on('news', function(data){
uname=data;
console.log("the information i got is" + uname);
});
socket.on('pass', function(data){
pwd=data;
console.log("the information i got is" + pwd);
if ( uname==123 && pwd ==321)
{
auth=true;
console.log("AUTHENTICATED USER");
}
});
socket.on('disconnect', function(data){
});
});
on console i am getting this
the information i got is123
the information i got is321
AUTHENTICATED USER
but the problem i am facing is if the user authorized it is not redirecting to another page
Upvotes: 0
Views: 984
Reputation: 177
Try sending url to client and let the client browser handle the redirection...
How about this?
Client:
$(function(){
$('#join').click( function() {
var uname = $('#uname').val();
alert(uname);
socket.emit('news', uname);
var pwd = $('#pwd').val();
socket.emit('pass', pwd);
socket.on('url', function (data) {
window.location.href=data;
});
});
});
Server :
app.get('/', function(req, res){
if(auth)
{
socket.emit('url', { msg: 'homePage' });
}
else
{
socket.emit('url', { msg: 'hiPage' });
}
});
Note:This is just a sample code,not executable one.
Upvotes: 1