是否可以在环回中注册用户之后立即获得访问令牌,而不必登录用户?如果是的话,你怎么做呢?正在使用回环3
发布于 2019-03-20 14:52:57
这是我现在的片段。您需要在您的common/models/account.js文件(或您选择的任何名称)中添加一个自定义远程方法,其中您的Account Model 承继是内置的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模型继承了内置的User模型,所以需要将访问控制列表(ACLs)打开到$everyone。
所以您的common/models/account.json文件应该如下所示:
{
"name": "Account",
"base": "User",
"idInjection": true,
"properties": {},
"validations": [],
"relations": {},
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"property": "createAndLogin"
}
],
"methods": []
}发布于 2019-03-20 08:39:21
我将向后远钩远程方法添加一个users/create,因此在成功调用它之后,您可以使用可能从request对象获得的密码调用User.login() (以获取访问令牌)。因此,在注册请求之后,您将在响应中获得访问令牌。
https://stackoverflow.com/questions/55255393
复制相似问题