2016-11-14 156 views
0

我已经将自己的登录功能写入验证用户名/密码的服务,我希望仍然使用LoopBacks用户模型。问题是获得一个accesstoken(并保持一个用户登录在LB应用程序中)我需要提供一个密码。在Loopbacks数据源中存储密码不是一个选项。我的问题,我如何告诉Loopback用户实际上已经过验证,并且可以在没有密码的情况下登录?根据我在User.Login代码中可以看到的内容,不存在跳过密码并仅登录用户的选项。跳过用户登录的密码

这一切都以编程方式完成,我使用以用户模型为基础的自定义用户模型。下面是一个简单的例子:

CustomUser.beforeRemote('login', function(ctx, unused, next) { 
    UserService.externalLogin(ctx.args.credentials.username ,ctx.args.credentials.password).then(function(response){ 
     //Go to user.login 
     if(response.authorized){ 
     next(); 
     } 
    }); 
    }); 

回答

2

不知道这是最好的做法,但我真的添加自定义登录入口点custom-user.js

CustomUser.customLogin = function (email, cb) { 
     var tokenTimeToLive = 12096000; //in ms 
     CustomUser.findOne({ 
      where: {email: email} 
     }, function (err, user) { 
      if (err) { 
       return cb(err, null); 
      } else { 
       if (user) { 
        user.createAccessToken(tokenTimeToLive, function (error, token) { 
         return cb(error, token); 
        }); 
       } else { 
        return cb(new Error("No User found"), null); 
       } 
      } 
     }); 
    }; 

    CustomUser.remoteMethod('customLogin', { 
     accepts: {arg: 'email', type: 'string', required: true}, 
     returns: {arg: 'credentials', type: 'object', root: true}, 
     description: "Custom login entry" 
    }); 

而且这种方法自定义ACL在custom-user.json

... 
"acls": [ 
    { 
     "accessType": "*", 
     "principalType": "ROLE", 
     "principalId": "$everyone", 
     "permission": "DENY" 
    }, 
    { 
     "accessType": "READ", 
     "principalType": "ROLE", 
     "principalId": "$authenticated", 
     "permission": "ALLOW" 
    }, 
    { 
     "accessType": "WRITE", 
     "principalType": "ROLE", 
     "principalId": "$authenticated", 
     "permission": "ALLOW" 
    }, 
    { 
     "principalType": "ROLE", 
     "principalId": "$everyone", 
     "permission": "ALLOW", 
     "property": "customLogin" 
    } 
    ] 
... 

当然,你可能希望使用另一个字段比email之一,但我认为它也是一个主键环回User模型。 但是,当您发布新用户时,您将始终需要为用户发布编程生成的密码。

+1

感谢您的解决方案,F3L1X79,我希望绕过生成令牌部分。我最终得到的是这种混合体,并且仍然使用登录功能,在用户被授权的情况下我生成令牌。 :) – Undrium