gpt4 book ai didi

node.js - 尝试注册新用户或登录时出错

转载 作者:行者123 更新时间:2023-12-03 22:38:23 26 4
gpt4 key购买 nike

出于某种原因,每当我向路由发出请求以创建新用户时,尽管用户已创建,但我会收到“此电子邮件帐户已在使用中”,而当我尝试登录时,我会收到“登录信息不正确”。我在登录时控制台记录了 req.body 并且我记录了对象,看起来没问题。

我的 AuthenticationController.js

const {User} = require('../models')
const jwt = require('jsonwebtoken')
const config = require('../config/config')

function jwtSignUser (user) {
const ONE_WEEK = 60 * 60 * 24 * 7
return jwt.sign(user, config.authentication.jwtSecret, {
expiresIn: ONE_WEEK
})
}

module.exports = {
async register (req, res) {
try {
const user = await User.create(req.body)
const userJson = user.toJSON()
res.send({
user: userJson,
token: jwtSignUser(userJson)
})
} catch (err) {
res.status(400).send('This email account is already in use.')
}
},
async login (req, res) {
console.log(req.body)
try {
const {email, password} = req.body
const user = await User.findOne({
where: {
email: email
}
})

if (!user) {
return res.status(403).send({
error: 'The login information was incorrect'
})
}

const isPasswordValid = await user.comparePassword(password)
if (!isPasswordValid) {
return res.status(403).send({
error: 'The login information was incorrect'
})
}

const userJson = user.toJSON()
res.send({
user: userJson,
token: jwtSignUser(userJson)
})
} catch (err) {
res.status(500).send({
error: 'An error has occured trying to log in'
})
}
}
}

console.log(user) 给了我以下信息:
User {
dataValues:
{ id: 41,
username: 'testing',
email: 'testing@gmail.com',
password:
'$2a$08$J6/psKuCltiCpCll2rjz4OaCylud0zq/wKnjKK8Udmm.bFU3c2Tt6',
firstName: 'test',
lastName: 'ing',
createdAt: 2019-02-26T15:47:09.133Z,
updatedAt: 2019-02-26T15:47:09.133Z },
_previousDataValues:
{ id: 41,
username: 'testing',
email: 'testing@gmail.com',
password:
'$2a$08$J6/psKuCltiCpCll2rjz4OaCylud0zq/wKnjKK8Udmm.bFU3c2Tt6',
firstName: 'test',
lastName: 'ing',
createdAt: 2019-02-26T15:47:09.133Z,
updatedAt: 2019-02-26T15:47:09.133Z },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: false,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: { email: 'testing@gmail.com' },
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
indexes: [],
name: { plural: 'Users', singular: 'User' },
omitNull: false,
hooks:
{ beforeCreate: [Array],
beforeUpdate: [Array],
beforeSave: [Array] },
sequelize:
Sequelize {
options: [Object],
config: [Object],
dialect: [SqliteDialect],
queryInterface: [QueryInterface],
models: [Object],
modelManager: [ModelManager],
connectionManager: [ConnectionManager],
importCache: [Object],
test: [Object] },
uniqueKeys: { Users_email_unique: [Object] } },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes:
[ 'id',
'username',
'email',
'password',
'firstName',
'lastName',
'createdAt',
'updatedAt' ] },
__eagerlyLoadedAssociations: [],
isNewRecord: false }

console.log(req.body) 给了我这个:
{ email: 'testing@gmail.com',
password: 'testing99',
username: 'testing',
firstName: 'test',
lastName: 'ing' }

他们有相同的信息,但无论如何它给了我一个错误。

最佳答案

我假设您正在使用 mongoose ,根据文档 https://mongoosejs.com/docs/api.html#model_Model.findOne findOne 正在返回 Query 对象,因此验证可能会失败。
检查调试器甚至是简单的 console.log 以查看登录函数中 user 变量的内容。

所以如果用户不存在,你得到的 Query 对象不是空的,因为 mongoose 用一些函数填充了这个对象。

你可以尝试使用 lean() ,查看这篇文章 http://www.tothenew.com/blog/high-performance-find-query-using-lean-in-mongoose-2/

关于node.js - 尝试注册新用户或登录时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54888274/

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