我需要 Loopback 远程方法到 return null
I need Loopback remote method to return null
所以我正在学习 NodeJs 的环回,并且我正在尝试使用 bcrypt 库进行身份验证。
如果找不到用户或密码不匹配,我需要我的登录远程方法return一个空值。
我的登录实现是:
User.login = function(username, password,cb){
User.findOne({where:{username: username}}, function(err, user){
cb(null, user);
console.log(JSON.stringify(user));
if(user === null){
console.error('User not found');
return null;
}
console.log('User found');
if(!bcrypt.compareSync(password, user.password)){
console.error('Illegal Password');
user = null;
console.log('USER SHOULD BE NULL =======> '+JSON.stringify(user));
return user;
}
console.log('User Authenticated');
return user;
});
}
事实上,如果找到用户,应该为 null 的 console.log 实际上是 null,但是在 auth.service 函数中,如果密码不匹配,它应该接收 null 值一个用户。
login(username: string, password: string) {
return this.http.post(this.configService.apiUrl + '/users/login', { username: username, password:password })
.pipe(map(user =>{
console.log('AUTHSERVICE USER SHOULD BE NULL ===========> '+ JSON.stringify(user));
//THIS USER IS NEVER NULL IF USER IS FOUND!!! EVEN IF PASSWORD IS NOT MATCHED
if(!user || user === null){
console.error('Invalid credentials');
return;
}
sessionStorage.setItem('currentUser', JSON.stringify(user));
this.loggedIn.next(true);
this.router.navigate(['/']);
return user;
}));
}
我确定我在这里遗漏了一些东西,如有任何帮助,我们将不胜感激。
谢谢
请注意 LoopBack 远程方法是异步的,因此结果必须通过第二个回调参数传递,而不是作为 return 值传递。
User.login = function(username, password,cb){
User.findOne({where:{username: username}}, function(err, user){
// ** DON'T FORGET TO HANDLE ERRORS **
if (err) return cb(err);
console.log(JSON.stringify(user));
if(user === null){
console.error('User not found');
// ** THE FIRST ARG IS "error", THE SECOND ARG IS "result"
cb(null, null);
}
console.log('User found');
if(!bcrypt.compareSync(password, user.password)){
console.error('Illegal Password');
user = null;
console.log('USER SHOULD BE NULL =======> '+JSON.stringify(user));
return cb(null, null);
}
console.log('User Authenticated');
cb(null, user);
});
}
解释您观察到的行为:在您的登录实施中,您总是 return findOne
找到的用户,请参见此处:
User.findOne({where:{username: username}}, function(err, user){
cb(null, user);
// none of the code below matters, because the result
// has been already set via `cb(null, user)`
}
所以我正在学习 NodeJs 的环回,并且我正在尝试使用 bcrypt 库进行身份验证。 如果找不到用户或密码不匹配,我需要我的登录远程方法return一个空值。
我的登录实现是:
User.login = function(username, password,cb){
User.findOne({where:{username: username}}, function(err, user){
cb(null, user);
console.log(JSON.stringify(user));
if(user === null){
console.error('User not found');
return null;
}
console.log('User found');
if(!bcrypt.compareSync(password, user.password)){
console.error('Illegal Password');
user = null;
console.log('USER SHOULD BE NULL =======> '+JSON.stringify(user));
return user;
}
console.log('User Authenticated');
return user;
});
}
事实上,如果找到用户,应该为 null 的 console.log 实际上是 null,但是在 auth.service 函数中,如果密码不匹配,它应该接收 null 值一个用户。
login(username: string, password: string) {
return this.http.post(this.configService.apiUrl + '/users/login', { username: username, password:password })
.pipe(map(user =>{
console.log('AUTHSERVICE USER SHOULD BE NULL ===========> '+ JSON.stringify(user));
//THIS USER IS NEVER NULL IF USER IS FOUND!!! EVEN IF PASSWORD IS NOT MATCHED
if(!user || user === null){
console.error('Invalid credentials');
return;
}
sessionStorage.setItem('currentUser', JSON.stringify(user));
this.loggedIn.next(true);
this.router.navigate(['/']);
return user;
}));
}
我确定我在这里遗漏了一些东西,如有任何帮助,我们将不胜感激。 谢谢
请注意 LoopBack 远程方法是异步的,因此结果必须通过第二个回调参数传递,而不是作为 return 值传递。
User.login = function(username, password,cb){
User.findOne({where:{username: username}}, function(err, user){
// ** DON'T FORGET TO HANDLE ERRORS **
if (err) return cb(err);
console.log(JSON.stringify(user));
if(user === null){
console.error('User not found');
// ** THE FIRST ARG IS "error", THE SECOND ARG IS "result"
cb(null, null);
}
console.log('User found');
if(!bcrypt.compareSync(password, user.password)){
console.error('Illegal Password');
user = null;
console.log('USER SHOULD BE NULL =======> '+JSON.stringify(user));
return cb(null, null);
}
console.log('User Authenticated');
cb(null, user);
});
}
解释您观察到的行为:在您的登录实施中,您总是 return findOne
找到的用户,请参见此处:
User.findOne({where:{username: username}}, function(err, user){
cb(null, user);
// none of the code below matters, because the result
// has been already set via `cb(null, user)`
}