登录按预期方式工作并返回了会话数据,但除此之外,我无法使会话数据持久化。例如,当我调用getUser(如下)时,不会返回会话数据。
app.js(设置)
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan')
const request = require('request')
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
const app = express()
app.use(morgan('combined'))
app.use(bodyParser.json())
app.use(cors({
credentials: true
}))
const mongodb_conn_module = require('./mongodbConnModule');
var db = mongodb_conn_module.connect();
app.use(session({
secret: 'work hard',
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
maxAge: 24 * 60 * 60 * 1000
},
store: new MongoStore({
mongooseConnection: db,
ttl: 24 * 60 * 60 // Keeps session open for 1 day
})
}));
app.js(登录)
app.post('/login', (req, res) => {
if (req.body.logEmail && req.body.logPassword) {
Users.authenticate(req.body.logEmail, req.body.logPassword, function (error, user) {
if (error || !user) {
var err = new Error('Wrong email or password.');
err.status = 401;
res.send("Wrong email or password.");
} else {
req.session.userId = user._id;
res.send({
success: true,
sessionInfo: req.session
});
}
});
} else {
var err = new Error('All fields required.');
err.status = 400;
res.send("All fields required.");
}
})
app.js(getUser)
app.get('/get_user', (req, res) => {
res.send(req.session)
})
UsersService.js(模型)
var mongoose = require("mongoose");
var bcrypt = require('bcrypt');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
userName: {
type: String,
unique: true,
required: true,
trim: true
},
email: {
type: String,
unique: true,
required: true,
trim: true
},
password: {
type: String,
required: true,
},
passwordConf: {
type: String,
required: true,
}
});
//authenticate input against database
UserSchema.statics.authenticate = function (email, password, callback) {
Users.findOne({ email: email })
.exec(function (err, user) {
if (err) {
return callback(err)
} else if (!user) {
var err = new Error('User not found.');
err.status = 401;
return callback(err);
}
bcrypt.compare(password, user.password, function (err, result) {
if (result === true) {
return callback(null, user);
} else {
return callback();
}
})
});
}
// hashing a password before saving it to the database
UserSchema.pre('save', function (next) {
var user = this;
bcrypt.hash(user.password, 10, function (err, hash) {
if (err) {
return next(err);
}
user.password = hash;
next();
})
});
var Users = mongoose.model("Users", UserSchema);
module.exports = Users;
谢谢!
答案 0 :(得分:0)
经过大量工作,我只是解决了相同的问题,因为在app.js(设置)中订购不佳,因此通过此代码编辑app.js(设置)希望对您有所帮助:)
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan')
const request = require('request')
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
const app = express()
app.use(session({
secret: 'work hard',
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
maxAge: 24 * 60 * 60 * 1000
},
store: new MongoStore({
mongooseConnection: db,
ttl: 24 * 60 * 60 // Keeps session open for 1 day
})
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(morgan('combined'))
app.use(cors({
credentials: true
}))
});
app.use(cors({
credentials: true
}))
const mongodb_conn_module = require('./mongodbConnModule');
var db = mongodb_conn_module.connect();
}));