0

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

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

我什么都试过了,但没有运气

提前致谢...

4

2 回答 2

0

Considering that you want to redirect the user, it would make more sense to check for the cookie within an app.get callback, because you now have access to the response object.

If you want to intercept every request, simply put a block like this in your code:

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
});
于 2013-01-20T21:24:05.440 回答
0

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

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>

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

于 2013-01-21T06:12:11.247 回答