gpt4 book ai didi

javascript - 由于某种原因, Mongoose 只保存密码哈希而不是所有字段

转载 作者:行者123 更新时间:2023-11-30 13:46:44 25 4
gpt4 key购买 nike

我有以下代码:

var express = require('express');
const app = express();
const mongoose = require('mongoose');
var bcrypt = require('bcrypt');
const jwt2 = require('jsonwebtoken');
const User = require('./models/user.js');
app.post('/api/login', async function (req, res){
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
console.log(hashedPassword);
console.log(await bcrypt.compare('testtest',hashedPassword));
var user = new User({ id: req.body.id, username: req.body.username, password: hashedPassword });
user.save(function (err, User) {
if (err) return console.error(err);
console.log("Saved successfully");
});
// TODO: Implement proper user check!
if (user.username !== 'test' && await bcrypt.compare('testtest', hashedPassword) !== true )
throw TypeError("Bah! Wrong username and password!");
// ----TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
jwt2.sign({user}, 'secrethere', { expiresIn: '15min'}, (err, token) =>{
res.json({
token
});
});
} catch (err) {
res.status(500).send()
console.log(err);
}

});

我的 user.js:

const mongoose = require('mongoose');

const userSchema = mongoose.Schema({

loginId: String,
firstname: String,
lastname: String,
eMail: String,
password: String,
active: Boolean

});

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

问题是代码只在数据库中保存密码散列,即使它被定义为保存更多(id、用户名、密码)。我做错了什么?

最佳答案

你使用'id'和'username'

var user = new User({ id: req.body.id, username: req.body.username, password: hashedPassword });

并且您的架构不包含这些字段
尝试:

var user = new User({ loginId: req.body.id, firstname: req.body.username, password: hashedPassword });

关于javascript - 由于某种原因, Mongoose 只保存密码哈希而不是所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59159412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com