在环回中注册后获取访问令牌

时间:2019-03-20 07:19:30

标签: node.js loopbackjs reactjs-native

是否可以在环回注册用户后立即获得访问令牌而无需登录用户?如果是这样,您将如何处理?正在使用环回3

2 个答案:

答案 0 :(得分:1)

我会在users/create远程方法中添加一个after remote hook,因此,在成功调用它之后,您可以使用可能获得的密码调用User.login()(获取访问令牌)。来自request对象。因此,在注册请求之后,您将在响应中获得访问令牌。

答案 1 :(得分:0)

这是我当前的摘录。 您需要在 common/models/account.js 文件(或您选择的任何名称)中添加自定义远程方法,其中Account模型inherits内置{{1 }}模型:

User

编辑:由于module.exports = function (Account) { Account.createAndLogin = function (data, cb) { if (!data || !data.password) { return cb(new Error("Attribute 'password' is mandatory to create a new user.")); } Account.create(data, function (err, account) { if (err) { return cb(err, null); } Account.login({email: data.email, password: data.password}, function (err, token) { if (err) { return cb(err, null); } cb(err, { id: token.id, ttl: token.ttl, created: token.created, userId: token.userId, account: account }); }); }); }; Account.remoteMethod('createAndLogin', { description: "Create and login in one remote method", accepts: {arg: 'data', type: 'object', required: true, http: {source: 'body'}, description: 'Model instance data'}, returns: {arg: 'accessToken', type: 'object', root: true, description: 'User Model'}, http: {verb: 'post'} }); }; 模型继承了内置的Account模型,因此您需要打开访问控制列表( ACL )到 $ everyone < / strong>。

因此,您的 User 文件应如下所示:

common/models/account.json