标签: passport-local-mongoose

如何使用passport-local-mongoose将多个对象保存到数据库?

我想使用passport-local-mongoose节点包将用户注册到我的mongoDB数据库,我对此没有问题,但我想要的是向数据库添加更多的用户名和密码:例如(他们的第一个和最后一个姓名、他们的角色以及几乎所有用户需要的东西)

我怎么能用passport-local-mongoose来做到这一点,或者除了那个包还有什么其他方法,我已经尝试将其他对象添加到数据库中,但由于某种原因它不起作用。这是我尝试将额外对象放入数据库的方法:

app.post('/register', function(req, res){
    User.register({username: req.body.username}, req.body.password, function(err, user) {
        if (err) { 
            console.log(err);
            res.redirect('/register')
         } else{
            passport.authenticate('local')(req, res, function(){
                User.updateOne({ username: req.body.username }, { $set: { firstName: 'firstnName', lastName: 'lastName' } })
                res.redirect('/secrets')
            })
           };
      });
})
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我使用 mongoose 的 updateOne 函数手动设置新值,但由于某种原因,我没有在数据库中看到它们。而且我在 User 模型架构中包含了名字和姓氏,这是架构:

const userSchema = new mongoose.Schema ({
    email: String,
    password: String,
    firstName: String,
    lastName: String
})

Run Code Online (Sandbox Code Playgroud)

有什么建议?

javascript mongoose mongodb node.js passport-local-mongoose

5
推荐指数
1
解决办法
127
查看次数

Passport-local-mongoose serializeUser 类型不正确

我正在尝试使用 Passportjs 和 mongoose 进行身份验证,但我很难使用打字稿获得正确的类型。

Passport.use(UserModel.createStrategy())
Passport.serializeUser(UserModel.serializeUser()) // <---- Error
Run Code Online (Sandbox Code Playgroud)

我收到错误:

No overload matches this call.
  Overload 1 of 2, '(fn: (user: User, done: (err: any, id?: any) => void) => void): void', gave the following error.
    Argument of type '(user: PassportLocalModel<User>, cb: (err: any, id?: any) => void) => void' is not assignable to parameter of type '(user: User, done: (err: any, id?: any) => void) => void'.
      Types of parameters 'user' and 'user' are incompatible.
        Type 'User' is …
Run Code Online (Sandbox Code Playgroud)

mongoose node.js typescript passport.js passport-local-mongoose

3
推荐指数
1
解决办法
1107
查看次数