我应该如何在我的应用程序中为未经授权的用户重定向或写一些东西
如果未定义 cookie 或 cookie 值为空,我想重定向页面
这是我的脚本,我指定了我认为应该添加的内容。
var express = require('express'),
app = express(),
memcache = require("memcache"),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server),
co = require("./cookie.js"),
codein = require("node-codein");
//check if user loggedin
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.configure(function (){
io.set('authorization', function (data, accept) {
var cookieManager = new co.cookie(data.headers.cookie);
var client = new memcache.Client(11211, "localhost");
client.connect();
client.get("sessions/"+cookieManager.get("sec_session_id"), function(error, result){
if(result){
var session = JSON.parse(result);
user = JSON.parse(session.name);
user = user.username;
}
if (typeof result === 'undefined' || (!result)) {
//------------> here if no cookie redirect to noaccess.html or just write you have no access to this page
return accept('No cookie transmitted.', false);
} else {
//------------> here if there is cookie then let users to see index.html
data.sessionID = cookieManager.get("sec_session_id");
accept('cookie recieved', true);
}
io.on('connection', function(socket) {
//console.log(socket.handshake.sessionID);
});
});
});
});
server.listen(3000);
我什么都试过了,但没有运气
提前致谢...