使用connect-session-sequelize会话不是永久性的

时间:2019-05-08 10:41:16

标签: javascript node.js express session sequelize.js

我正在使用express在node.js中使用仪表盘analisys开发一个网站 我在connect-session-sequelize上遇到了一些问题,因为数据是写在会话表中的,但是一旦我关闭标签并打开一个新的标签,我的网站就会问我总是用电子邮件和密码登录

//session  
const db = new Sequelize('dbname', 'postgres', 'psw', {
  host: 'localhost',
  port:port,
  dialect: 'postgres',
  storage: './session.postgres',
});

var myStore = new SequelizeStore({
  db:db
})


app.use(session({
  secret: 'keyboard cat', // TODO ENV
  store: myStore,
  resave: false, 
  saveUninitialized:true,
  proxy: true ,
  cookie: {
    maxAge: new Date(Date.now() + (30 * 86400 * 1000)),  // TODO : env  --> one hour
    sameSite: true,
    secure: false // TODO : TRUE IN PRODUCTION*/

  }
}))

myStore.sync();

// login 
app.post("/login",async (req, res) => {
  let email = req.body.email;
  let password = req.body.password;
  console.log(password);

  try {
    const user = await newUser.findOne({
      raw: true,
      attributes: ["password", "administrator", "id"],
      where: { email: req.body.username }
    });
    console.log("id da db " + user.id);
    console.log("password da db " + user.password);

    const authenticated = await correctPassword(password, user.password);
    console.log(authenticated);

    if (authenticated === true && user.administrator === false) {
      req.session.userId = user.id; // creo l'id per la sessione
      res.redirect("/dashboard");
    } else {
      if (authenticated === true && user.administrator === true) {
        req.session.userId = user.id;
        console.log('id sessione' + req.session.userId);
        console.log('sessione' + req.sessionID)
        res.redirect("/adminDashboard");   
      } else {
        res.render("login1", {
          user: undefined,
          success: true,
          successs: true,
          error: "Password or Email are incorrect.Try again "
        });

      }
    }
  } catch (e) {
    res.render("login1", {
      user: undefined,
      success: true,
      successs: true,
      error: "Password or Email are incorrect.Try again "
    });
  }
});



实际结果ID:如果我已经登录,则关闭选项卡,打开一个新选项,然后输入一个新的数字localhost / login它将保留在localhost / login,但是如果我编写localhost / dashboard(登录后的页面),我将可以进入。

预期结果是:如果我已经登录,请关闭选项卡,然后打开一个新选项卡,然后输入一个本地主机名/登录名,它会将我重定向到本地主机/仪表板中

0 个答案:

没有答案