Express.JS上的会话Cookie

时间:2019-04-01 02:36:56

标签: javascript node.js express cookies

所有人!

我正在使用Express-Session来管理服务器端会话,因此,当我获取用户数据并将其设置为session变量时,它具有完美的价值,但是如果我尝试在其他文件中使用它们,则只能获得Cookie标头,而不是我的变量。如果需要的话,我正在使用邮递员。

我的server.js

var express = require('express'),
    bodyparser = require('body-parser'),
    validator = require('express-validator'),
    session = require('express-session'),
    app = express();

var consign = require('consign');
app.use(bodyparser.urlencoded({ extended: true }));
app.use(bodyparser.json())
app.use(validator())
app.use(session({
    secret: 'iau#$%#$Hgs@#t4',
    resave: false,
    saveUninitialized: false
}))

consign()
    .include('./src/routes')
    .then('./src/controllers')
    .then('./src/config/conexao.js')
    .then('./src/models')
    .then('./src/utils')
    .into(app);


app.listen(3000, function () {
    console.log("Servidor ON");
});

module.exports = app;

使用户满意的功能:

module.exports.autenticar = function (app, req, res) {

    var valido = app.src.utils.functions.validaUsuarioLogin(app, req);
    if (valido == true) { //true significa que não deu erro
        const conexao = new app.src.config.conexao.Conexao(),
            usuario = new app.src.models.usuariosDAO.UsuariosDAO()
        let dadosusuario = req.body;

        usuario.autenticar(conexao, dadosusuario, (err, result) => {
            if (err) {
                res.json(conexao.trataErros(err))
            } else if (result.length != 0) {
                //Permissoes Gerais
                req.session.exibecadastros = result[0].exibecadastros
                //Permissoes para Tarefas
                req.session.aprovatarefa = result[0].aprovatarefa;
                req.session.cancelatarefa = result[0].cancelatarefa
                req.session.alteratarefa = result[0].alteratarefa
                req.session.criatarefa = result[0].criatarefa
                //Permissao para Cargos
                req.session.criacargo = result[0].criacargo
                req.session.atualizacargo = result[0].atualizacargo
                req.session.deletacargo = result[0].deletacargo
                req.session.visualizacargos = result[0].visualizacargos
                console.log(req.session) //here we have data from the variables above
                //   res.json(result)
            }
            else {
                let msg = {
                    msg: "Nenhum usuario encontrado com essas informações."
                }
                res.send(msg)
            }
        }) //saindo da função de autenticação
    } else {
        res.send(valido)
    }
}

我要保护的路线:

module.exports = function (app) {
    app.get('/cargos', function (req, res) {
        console.log(req.session) // Here I have only cookie headers
        app.src.controllers.cargos.getCargos(app, req, res)
    });
}

console.log(req.session)

Session {
  cookie:
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true } }

1 个答案:

答案 0 :(得分:1)

根据我自己在处理express.js / cookies / session和通过Postman进行测试方面的经验,我发现它不能维护cookie。我一直不得不自己将Cookie /会话添加到Postman或使用实际网站上的Chrome中的Postman插件捕获会话。

尝试直接在浏览器中执行这些步骤,或确保您添加返回到您发送的下一个请求的cookie。