处理用户名并在nodejs-express中传递会话

时间:2014-06-24 01:08:49

标签: javascript node.js session express session-cookies

我有一个nodejs-express app正在运行,并且是新手。

在我的主页('/')中,我有以下内容:

app.get('/', function(req, res){
    // check if the user's credentials are saved in a cookie //
        if (req.cookies.user == undefined || req.cookies.pass == undefined){
            res.render('login', { title: 'KrishiMoney' });
        }   else{
    // attempt automatic login //
            AM.autoLogin(req.cookies.user, req.cookies.pass, function(o){
                if (o != null){
                    req.session.user = o;
                    res.redirect('/home');
                }   else{
                    res.render('login', { title: 'KrishiMoney' });
                }
            });
        }
    });

现在当我打印req.cookies.user时,它给了我username.Anyhow,somwhere沿着我有管理路线的路线:

app.get('/admin', function(req, res){
        var admin_username = 'admin'
        var admin_password = 'nansat'
        console.log(req.cookies.user);
        //if (req.session.user != admin_username && req.session.pass != admin_password) res.redirect('404');
        (res.render('admin_login', {title : 'Admin Page'}));
        });

这里当我输入相同的命令req.cookies.user时,它给了我undefined。我如何保留用户并通过路线?

0 个答案:

没有答案