【问题标题】:redirect if no cookie in express.js and socket.io如果 express.js 和 socket.io 中没有 cookie,则重定向
【发布时间】:2013-01-21 14:12:11
【问题描述】:

我应该如何在我的应用程序中为未经授权的用户重定向或写一些东西

如果没有定义 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);

我尝试了一切,但没有运气

提前谢谢...

【问题讨论】:

  • 恕我直言,你根本不应该允许没有 cookie 的 socket.io 连接,你也可以使用一些授权插件来表达,比如passport
  • @dinchev 如果你看到我用 cookie 模块解析 cookie 亲爱的 drinchey,我不允许没有任何 cookie。 /跨度>

标签: node.js redirect express socket.io


【解决方案1】:

考虑到您想要重定向用户,在 app.get 回调中检查 cookie 会更有意义,因为您现在可以访问响应对象。

如果你想拦截每一个请求,只需在你的代码中放一个这样的块:

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

    if(req.cookies){ // or req.cookies['cookiename'] if you want a specific one
        res.render("index.html");
    }   

    else{
        res.render("noaccess.html");
    }
    //You can optionally put next() here, depending on your code
});

【讨论】:

  • 更好...res.render(req.cookies ? 'index.html' : 'noaccess.html')
【解决方案2】:

在我的情况下,如果授予访问权限,我认为我需要发出,然后我们在服务器端有这个

io.on('connection', function(socket) {
            //console.log(socket.handshake.sessionID);
            socket.emit('access', 'access granted');
            });
    });

在客户端我们有这个表格

<html>
  <head>
    <title> Tying it all together demo</title>
    <script src='/socket.io/socket.io.js'></script>
    <script src='http://code.jquery.com/jquery-latest.js'></script>
    </head>
  <body>
    <p id="text">access denied</p>
    <script>
        var socket = io.connect('http://localhost:3000/');
        socket.on('access', function (data) {
            $("#text").html(data);
        });

    </script>
</body>

如果我们有访问权限,我们会在服务器中发出访问功能,拒绝访问文本将更改为授予访问权限

【讨论】:

    猜你喜欢
    • 2011-12-23
    • 2013-09-21
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多