登录身份验证不起作用或验证 nodejs 中的用户
login authentications is not working or verifying the user in nodejs
我正在尝试在 nodejs 中创建登录系统,我正在使用 bcrypt 进行密码哈希,但每次我尝试获取用户时都会出错,我认为它没有进行任何查询。
我就是这样尝试的
models/User.js
import mongoose, { Schema } from 'mongoose';
import bcrypt from 'bcrypt';
//Todo: add uniqueness and email validattions
const schema = new mongoose.Schema({
email: {
type: String,
required: true,
lowercase: true,
index: true,
},
passwordHash: {
type: String,
required: true
},
}, { timestamps: true });
schema.methods.isValidPassword = function isValidPassword(password) {
return bcrypt.compareSync(password, this.passwordHash)
}
export default mongoose.model('User', schema)
routes/auth.js
import express from 'express';
import User from '../models/User';
const router = express.Router();
router.post('/', (req, res) => {
const { credentials } = req.body;
User.findOne({ email: credentials.email }).then(user => {
if (user && user.isValidPassword(credentials.password)) {
res.json({ user: { email: user.email } });
} else {
res.status(400).json({ errors: { global: "Invalid credentials" } });
}
});
});
export default router;
它在检查的网络面板中给出了这个错误。
{errors: {global: "Invalid credentials"}}
errors: {global: "Invalid credentials"}
global: "Invalid credentials"
您正在 bcrypt
上按照 compatibility-note 使用 $2y$ bcrypt
This library supports a$ and b$ prefix bcrypt hashes. x$ and
y$ hashes are specific to bcrypt implementation developed for Jon
the Ripper. In theory, they should be compatible with b$ prefix.
Compatibility with hashes generated by other languages is not 100%
guaranteed due to difference in character encodings. However, it
should not be an issue for most cases.
您可以使用默认使用 $2y$ 的不同 brcypt 模块 twin-bcrypt
我正在尝试在 nodejs 中创建登录系统,我正在使用 bcrypt 进行密码哈希,但每次我尝试获取用户时都会出错,我认为它没有进行任何查询。
我就是这样尝试的
models/User.js
import mongoose, { Schema } from 'mongoose';
import bcrypt from 'bcrypt';
//Todo: add uniqueness and email validattions
const schema = new mongoose.Schema({
email: {
type: String,
required: true,
lowercase: true,
index: true,
},
passwordHash: {
type: String,
required: true
},
}, { timestamps: true });
schema.methods.isValidPassword = function isValidPassword(password) {
return bcrypt.compareSync(password, this.passwordHash)
}
export default mongoose.model('User', schema)
routes/auth.js
import express from 'express';
import User from '../models/User';
const router = express.Router();
router.post('/', (req, res) => {
const { credentials } = req.body;
User.findOne({ email: credentials.email }).then(user => {
if (user && user.isValidPassword(credentials.password)) {
res.json({ user: { email: user.email } });
} else {
res.status(400).json({ errors: { global: "Invalid credentials" } });
}
});
});
export default router;
它在检查的网络面板中给出了这个错误。
{errors: {global: "Invalid credentials"}} errors: {global: "Invalid credentials"} global: "Invalid credentials"
您正在 bcrypt
This library supports a$ and b$ prefix bcrypt hashes. x$ and y$ hashes are specific to bcrypt implementation developed for Jon the Ripper. In theory, they should be compatible with b$ prefix.
Compatibility with hashes generated by other languages is not 100% guaranteed due to difference in character encodings. However, it should not be an issue for most cases.
您可以使用默认使用 $2y$ 的不同 brcypt 模块 twin-bcrypt