[英]"Error: Illegal arguments: string, undefined" and stop server in node JS
我正在嘗試在節點 JS 中構建日志記錄應用程序。 在這里密碼驗證應用程序不能正常工作。 當我輸入用戶名和密碼時,出現以下錯誤並停止服務器。
this is the error.
這是身份驗證部分的代碼
passport.use(new LocalStrategy(
function(username, password, done) {
User.getUserByUsername(username, function(err, user){
if(err) throw err;
if (!user) {
return done(null, false, {message: 'Unknown user'});
}
User.comparePassword(password, user.password, function(err, isMatch){
if(err) throw err;
if (isMatch) {
return done(null, user);
} else {
return done(null, false, {message: 'Invalid password'});
}
});
});
}));
此代碼適用於未知用戶。 但它不適用於比較用戶名和密碼。 我在這里看不到任何錯誤。 我需要幫助解決這個問題。
我在這里發現了問題。 這不是關於代碼的事情。
問題是我用相同的用戶名和不同的密碼注冊了兩個用戶。 然后當我嘗試使用用戶名和一個密碼登錄時,它發生了這個錯誤並停止了服務器。
因為在用戶輸入的用戶名中查找密碼存在尷尬的情況。 因為有兩個密碼相同的用戶名。
以宇宙程序員的名義
就我而言,我忘記選擇密碼,因為在數據庫中密碼是 ((select: false))
此應用程序代碼
const user = await User.findOne({email}).select("+password")
我忘了將 ((.select("+password"))) 附加到 findOne
我收到了這個錯誤; 錯誤:非法參數:字符串,未定義
和這個數據庫代碼
const User = new mongoose.Schema({
username:{
type:String,
required: [true,"نام کاربری ضروری است"]
},
email:{
type:String,
required: [true,"رایانامه ضروری است"],
unique: true,
match:[
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{1,3})$/,
"لطفا یک رایانامه صحیح وارد کنید"
]
},
password:{
type:String,
required:[true,"رمز ضروری است"],
minlegth: 5,
select: false
}
})
就我而言,我使用的是箭頭函數
userSchema.methods.comparePassword = async (enterdPassword) => {
return await bcrypt.compare(enterdPassword, this.password);
};
我轉換為正常功能
userSchema.methods.comparePassword = async function (enterdPassword) {
return await bcrypt.compare(enterdPassword, this.password);
};
解決了這個問題
就我而言,我正在使用社交登錄/注冊。 當用戶使用社交登錄選項進行注冊時,存儲的密碼值為“NULL”。
所以我只是添加了這個小檢查:
comparePassword: function(password, user){
if (!user.password)
return false;
return bcrypt.compareSync(password, user.password);
}
在
“模型/user.js”
里面比較密碼
module.exports.comparePassword = (candidatePassword, hash, callback) => {...)
添加此代碼:
bcrypt.hash(candidatePassword, 10, (err, hash) => {
if(err) {
throw err;
}
bcrypt.compare(candidatePassword, hash, (err, isMatch) => {
if(err) {
throw err;
}
callback(null, isMatch);
});
});
在這里,我們從登錄頁面獲取用戶名和密碼,並通過數據庫中的用戶名找到我們的用戶,然后將其加密密碼與用戶輸入的密碼進行匹配
passport.use(new LocalStrategy(
(username,password,done)=> {
db.users.findOne({username: username},(err, user)=> {
if(err) return done(err);
if(!user) {
return done(null,false,{message: 'Incorrect Username'});
}
bcrypt.compare(password, user.password,(err,isMatch)=> {
if(err) return done(err);
if(isMatch) {
return done(null, user);
} else {
return done(null, false,{message: 'Incorrect Password'});
}
});
});
}
));
您也需要將 await 應用於鹽和密碼分配。
像這樣,
const salt = await bcrypt.genSaltSync(10);
const password = await req.body.password;
您可以編寫這樣的代碼:After this.findOne({ select: [].......})... 希望對您有所幫助
async validateUserPassword(loginDto: AuthLoginDto): Promise<User> { const { mobile, email, password } = loginDto; const user = await this.findOne({ select: ['id', 'email', 'mobile', 'password', 'salt', 'status', 'logged_at'], where: [ { mobile: mobile }, { email: email } ] }); if (user && await user.validatePassword(password)) { const logged_at = { logged_at: new Date() } await this.update({ id: user.id }, logged_at) return user; } else { return null; } } async validatePassword(password: string): Promise<boolean> { const hash = await bcrypt.hash(password, this.salt); return hash === this.password; }
在我自己的情況下,我只想檢查舊密碼是否與 Db 中的密碼匹配但出現錯誤,這是我的代碼如下:
changePassword = asyncHandler (async (req: IGetUserAuthInfoRequest, res: Response) => {
const user = await User.findById(req.user._id)
const {oldPassword, password} = req.body
if(!user) {
res.status(400)
throw new Error("User not found, please signup")
}
// Validate
if(!oldPassword || !password) {
res.status(400)
throw new Error("Please add old and new password")
}
// Check if old password matches password in DB
const passwordIsCorrect = await bcrypt.compare(oldPassword, user.password)
// Save new password
if(user && passwordIsCorrect) {
user.password = password
await user.save()
res.status(200).send("Password change successful")
} else {
res.status(400)
throw new Error("Old password is incorrect")
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.