我们的登录模式只要求用户输入用户名和密码,电子邮件是可选的,但是环回用户模型需要电子邮件才能创建新用户。
有没有办法在Strongloop中创建新用户而无需输入电子邮件?
答案 0 :(得分:3)
目前,无法允许非唯一的用户电子邮件。一 原因是User.login支持电子邮件和用户名, 即您可以输入电子邮件和密码登录。
请见this link。
但不要担心,有一种丑陋的(或者可能是创造性的)方法来解决这个问题:
1.从common/models/base-user.json
中的用户模型扩展:
{
"name": "baseUser",
"base": "User",
"idInjection": true,
"hidden":["email"],
"properties": {
"username": {
"type": "string",
"required": true,
"index": {
"unique": true
}
}
}
}
2.在注册common/models/base-user.js
之前为每位用户创建随机电子邮件:
module.exports = function (BaseUser) {
BaseUser.beforeRemote('create', function (context, user, next) {
var req = context.req;
req.body.email =Date.now()+"a@b.cc";
next();
});
};
3.定义baseUser模型并隐藏server/config-model.json
"baseUser": {
"dataSource": "MongoDB",
"public": true
},
"User": {
"dataSource": "MongoDB",
"public": false
}
答案 1 :(得分:0)
根据文档,目前无法改变内置模型的必需属性:
https://docs.strongloop.com/display/public/LB/Customizing+models
答案 2 :(得分:0)
@viam had the right idea,但由于API随时间的变化,今天最新的Loopback(^ 3.17或^ 4)不推荐使用该答案的代码。
Specifically deprecated:BaseUser.beforeRemote('create',...
这是基于Ella API中更完整的实现的假设Loopback 3的更新基本用户:
module.exports = function (Ellauser) {
Ellauser.observe('before save', function filterProperties(ctx, next) {
let oInstance = ctx.instance;
if (oInstance) oInstance.email = oInstance.email || 'placeholder@example.com';
next();
});
};