我正在使用 Heroku/Mongolab(我已经用 MongoHQ 尝试过),标题显示了我的设置。这是我的 package.json:
"name": "metatf-web",
"version" : "0.0.3",
"dependencies": {
"express": "*",
"ejs": ">=0.4.2",
"passport": "*",
"passport-steam": "*",
"validator": "*",
"mongoose" : "*",
"connect-mongo" : "latest",
"mongodb" : "latest"
}
基本上,当我创建一个新用户时,我的数据库会覆盖数据库中的一个用户,但只是有时。这是我的用户架构:
/**
* Module dependencies
*/
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
/**
* User Schema
*/
module.exports = function() {
var Users = new Schema({
steamid : { type: String }
, regdate : { type: Date, default: Date.now }
, avatar : { type: String, default: "none" }
, isadmin : { type: String, default: "no" }
});
mongoose.model("Users", Users);
};
这是我用来确定用户是否在数据库中的函数,如果不是,则为:
var checkIfUserAddToDbIfNot = function(req, res, next) {
var steamID = req.user;
require('./controllers/user_controller').get(steamID, function(err, doc) {
console.log(doc);
if (!doc) { // User not found
require('./controllers/user_controller').create(steamID);
console.log('User added');
}
});
return next();
};
我的用户控制器(CRUD):
/**
* Module dependencies.
*/
var mongoose = require('mongoose')
, Users = mongoose.model("Users")
, user = new Users();
// Create
exports.create = function (steamID) {
if (steamID == 76561197991291041) { // Add me as an admin for when database resets.
user.isadmin = 'yes';
} else {
user.isadmin = 'no';
}
user.steamid = steamID;
user.save(function (err, callback) {
if (err) throw err;
console.log('User ' + user.steamid + ' added');
console.log('Admin: ' + user.isadmin);
});
};
// Read
exports.get = function (steamID, fn) {
Users.findOne({ steamid: steamID }, function (err, doc) {
if (err) return err;
if (doc) {
fn(null, doc);
} else {
fn(null, null); // User not found
}
});
};
这里是调用检查用户函数的地方:
app.get('/auth/steam/return',
passport.authenticate('steam', { failureRedirect: '/' }), checkIfUserAddToDbIfNot, function (req, res) {
res.redirect('/');
});
最后是我的 connect-mongo 东西:
app.use(express.session({
secret: 'dont be walmarting',
store: new MongoStore(sess_conf.db),
cookie: {
maxAge: new Date(Date.now() + 1209600000), // DO NOT CHANGE
expires: new Date(Date.now() + 1209600000) // DO NOT CHANGE
},
maxAge : new Date(Date.now() + 1209600000), // DO NOT CHANGE
expires: new Date(Date.now() + 1209600000) // DO NOT CHANGE
}));
这里的任何帮助将不胜感激,我一生都无法弄清楚为什么用户被覆盖。我曾尝试在站点中添加新用户之前清除 cookie,因为我认为这是 Connect-Mongo 会话,但这不起作用。感谢任何可以帮助我的人。
编辑:我正在下面初始化 Passport 会话:
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
// END APP CONFIG (annotated)