简体   繁体   English

我需要Loopback远程方法返回null

[英]I need Loopback remote method to return null

So I'm learning Loopback for NodeJs and I'm trying to do authentication using bcrypt lib. 因此,我正在学习NodeJ的回送,并尝试使用bcrypt lib进行身份验证。 I need my login remote method to return a null value if the user is not found or the password doesn't match. 如果找不到用户或密码不匹配,我需要我的登录远程方法返回空值。

My implementation of login is: 我的登录实现是:

  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; }); } 

In fact the console.log that should be null is actually null if the user is found, however in the auth.service function that should receive the null value if password doesn't match it does receives a 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; })); } 

I'sure I'm missing something here any help would be appreciated it. 我确定我在这里缺少任何东西,将不胜感激。 Thanks. 谢谢。

Please note that LoopBack remote methods are async, therefore the result must be passed via the second callback argument, not as a return value. 请注意,LoopBack远程方法是异步的,因此结果必须通过第二个回调参数传递,而不是作为返回值传递。

 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);
    });
}

To explain the behavior you are observing: in your implementation of login, you always return the user found by findOne , see here: 要解释您观察到的行为:在登录的实现中,始终返回由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)`
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM