快速会话值自动销毁

  • 本文关键字:会话 session mean-stack
  • 更新时间 :
  • 英文 :


我是平均堆栈的新手。我有一个问题。我使用了快速会话,但是当我设置会话时,我可以第一次获得会话值。下次调用相同的函数或其他函数时,我无法获取会话值。会话值自动销毁。我已经尝试了快速会话,快速cookie和快速会话cookie,但没有成功。

有什么解决办法吗?

服务器.js代码:

const express = require('express');
path = require('path');
bodyParser = require('body-parser');
cors = require('cors');
mongoose = require('mongoose');
config = require('./config/DB');
const app = express();
var session = require('express-session');
var cookieParser = require('cookie-parser');
app.use(cookieParser());
app.use(session({
secret: "Shh, its a secret!",
resave: false, 
saveUninitialized: true, 
cookie: { secure: true }  
}));
mongoose.Promise = global.Promise;
const signUpRoutes = require('./routes/signup.route');
const logInRoutes = require('./routes/login.route');
app.use(bodyParser.json());
app.use(cors());
const port = process.env.PORT || 4000;
app.use('/signup', signUpRoutes);
app.use('/login', logInRoutes);
const server = app.listen(port, function(){
console.log('Listening on port ' + port);
});

login.route.js:

const express = require('express');
var logInRoutes = express.Router();
mongoose = require('mongoose');
const bcrypt = require('bcrypt');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
// Defined login route
logInRoutes.route('/login').post(function (req, res) {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var check_dbo = db.db('eclipsia');
var given_email=req.body.email;
var given_password=req.body.password;
check_dbo.collection("company").findOne({company_email: given_email}, 
function(err, result) {
if(result){
if(bcrypt.compareSync(given_password, result.password)) {
req.session.email= result.company_email;
req.session.company_id= result._id;
req.session.save();
if(req.session.email){
res.status(200).json({'return_data': '1' }); 
}else {
res.status(200).json({'return_data': '0'});
}
}else { 
res.status(200).json({'return_data': '2'});
}
}else{ 
res.status(200).json({'return_data': '3'});
}
});
});
});
logInRoutes.route('/login_check').get(function (req, res) {
if(req.session.email){
res.status(200).json({'return_data': '1'}); 
} else {
res.status(200).json({'return_data': '0'});
}
});
module.exports = logInRoutes;

您可以设置在快速会话中过期会话的最长期限

app.use(session({
secret: 'keyboard cat',
cookie: { secure: false,expires:  new Date(Date.now() + (90 * 86400 * 1000)) },  //  session valid for 90 days
resave: true,
saveUninitialized: true

}((;

相关内容

  • 没有找到相关文章

最新更新