简体   繁体   English

使用 NodeJS 和 MongoDB 跟踪上次登录日期

[英]Keep track of last login date with NodeJS and MongoDB

I'm working on adding a last login functionality to my Node app and can't seem to get it to work.我正在为我的 Node 应用程序添加最后登录功能,但似乎无法让它工作。 Here's what I've got for a mongoose user schema:以下是我为 mongoose user架构提供的内容:

userSchema = new mongoose.Schema({
    username: {
        type: String,
        unique: true
    },
    password: String,
    email: {
        type: String,
        unique: true 
    },
    avatar: String,
    firstName: String,
    lastName: String,
    lastLogin: {
        type: Date,
        default: Date.now
    },
    resetPasswordToken: String,
    resetPasswordExpires: Date,
    isAdmin: {
        type: Boolean,
        default: false
    }
});


userSchema.plugin(passportLocalMongoose);

userSchema.statics.newLogin = function login(id, callback) {
    return this.findByIdAndUpdate(id,{'$set' : { 'lastLogin' : Date.now()} }, { new : true }, callback);
 };


module.exports = mongoose.model("User", userSchema);

Here's my login route:这是我的登录路线:

router.post("/login", passport.authenticate("local", 
    {
        failureRedirect: "/login"
    }), function(req, res) {
    User.findOneAndUpdate(req.username, {lastLogin: Date.now()}, (err, data) => {
    if(err) console.log(err);
    else console.log("Successfully updated the lastLogin", data);

    res.redirect("/players");
  });
});

I've been able to get the initial date to stick when the account is created;我已经能够在创建帐户时获得初始日期; however, when I login with that same account the date remains unchanged.但是,当我使用同一个帐户登录时,日期保持不变。 What am I missing here?我在这里想念什么?

There are a few other questions with similar topics, but none seem to resolve my issue.还有一些其他类似主题的问题,但似乎没有一个可以解决我的问题。 Specifically, this question where I've implemented part of the solution to no avail.具体来说, 这个问题我已经实施了部分解决方案无济于事。 Thanks in advance for the help!在此先感谢您的帮助!

Here's the code currently being tested, example req.body:这是当前正在测试的代码,例如 req.body:

{ username: 'anyUserHere', password: 'anyPasswordHere' }

req.user:请求用户:

{
  isAdmin: true,
  _id: 5e9b301a6bb78973c9ec8fae,
  username: 'anyUserHere',
  salt: 'saltValue',
  hash: 'hashValue',
  __v: 0,
  avatar: '../images/admin.jpg',
  email: 'example@example.com',
  firstName: 'first',
  lastName: 'last',
  password: 'anyPasswordHere',
  lastLogin: 2020-05-22T18:35:50.941Z
}

So in this case, the 'anyUserHere' example should be the one being updated, but the update occurs to the first user in Mongo.因此,在这种情况下,“anyUserHere”示例应该是正在更新的示例,但更新发生在 Mongo 中的第一个用户身上。 Console output:控制台 output:

Successfully updated the lastLogin {
  isAdmin: false,
  _id: 5e939f988ced3e0428c8b521,
  username: 'test',
  __v: 0,
  lastLogin: 2020-05-22T18:38:59.836Z
}

Can you update the User.newLogin();你能更新User.newLogin(); with the below code and try使用以下代码并尝试

  User.newLogin(id, (err, data) => {
    if(err) console.log(err);
    else console.log("Successfully updated the lastLogin", data);
  });
router.post("/login", passport.authenticate("local", 
    {
        failureRedirect: "/login"
    }), function(req, res) {

    User.newLogin(id, (err, data) => {
    if(err) console.log(err);
    else console.log("Successfully updated the lastLogin", data);

    res.redirect("/players");
  });
});

Edit编辑

According to the current approach with findOneAndUpdate you need to make the following updated to the filter根据当前使用findOneAndUpdate的方法,您需要对filter进行以下更新

   User.findOneAndUpdate({username: req.username}, {lastLogin: Date.now()}, (err, data) => {
    if(err) console.log(err);
    else console.log("Successfully updated the lastLogin", data);

    res.redirect("/players");
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM