我试图使用扩展的环回用户模型和loopback-component-passport进行facebook登录。登录本身正在运行,但我不能使用我的自定义用户模型而不是内置用户"。
我采取的步骤:
- 使用slc loopback创建自定义用户模型:模型扩展"用户"
{
"name": "myuser",
"plural": "myusers",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"mytestproperty": {
"type": "string",
"default": "myteststring"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
- 使用新用户模型设置护照组件:
module.exports = function (app) {
var passportConfigurator = new PassportConfigurator(app);
passportConfigurator.init();
passportConfigurator.setupModels({
userModel: app.models.myuser,
userIdentityModel: app.models.UserIdentity,
userCredentialModel: app.models.UserCredential
});
passportConfigurator.configureProvider('facebook-login',
require('../../providers.json')['facebook-login']);
};
问题: 当我通过facebook登录时,护照组件仍然使用"用户"我的db.json存储中看到的模型:
{
"ids": {
"User": 2,
"UserCredential": 1,
"UserIdentity": 2,
"AccessToken": 2,
"ACL": 1,
"RoleMapping": 1,
"Role": 1,
"myuser": 1
},
"models": {
"User": {
"1": "{\"username\":\"facebook.13371337\",\"password\":\"blablabla\",\"email\":\"blablabla\",\"id\":1}"
},
"UserCredential": {},
"UserIdentity": {
"1": "{\"provider\":\"ALL MY IDENTITY INFO BLABLABLA}"
},
"AccessToken": {
"1337": "{\"id\":\"1337\",\"ttl\":1209600,\"created\":\"2017-03-01T09:34:51.965Z\",\"userId\":1}"
},
"ACL": {},
"RoleMapping": {},
"Role": {},
"myuser": {}
}
}
正如您所见"用户"填充了我新创建的用户和" myuser"是空的。
我是否错误地将环回用户与护照一起扩展的正确方法是什么? 任何有关示例的提示或参考都非常感谢!
答案 0 :(得分:4)
您必须扩展所有与护照相关的模型,以便将它们链接到您的自定义用户模型。
user.json
{
"name": "user",
"plural": "users",
"base": "User",
"relations": {
"accessTokens": {
"type": "hasMany",
"model": "accessToken",
"foreignKey": "userId"
},
"identities": {
"type": "hasMany",
"model": "userIdentity",
"foreignKey": "userId"
},
"credentials": {
"type": "hasMany",
"model": "userCredential",
"foreignKey": "userId"
}
},
"validations": [],
"acls": [],
"methods": []
}
用户identity.json
{
"name": "userIdentity",
"plural": "userIdentities",
"base": "UserIdentity",
"properties": {},
"validations": [],
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
},
"acls": [],
"methods": []
}
用户credential.json
{
"name": "userCredential",
"plural": "userCredentials",
"base": "UserCredential",
"properties": {},
"validations": [],
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
},
"acls": [],
"methods": []
}
访问token.json
{
"name": "accessToken",
"plural": "accessTokens",
"base": "AccessToken",
"properties": {},
"validations": [],
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
},
"acls": [],
"methods": {}
}
server.js(相关部分)
const PassportConfigurator = require('loopback-component-passport').PassportConfigurator
const passportConfigurator = new PassportConfigurator(app)
let providersConfig = require('./providers.json')
passportConfigurator.init()
passportConfigurator.setupModels({
userModel: app.models.user,
userIdentityModel: app.models.userIdentity,
userCredentialModel: app.models.userCredential
})
for (let s in providersConfig) { // Configure providers based on providers.json config
let c = providersConfig[s]
c.session = c.session !== false
passportConfigurator.configureProvider(s, c)
}
还有an example repository,可能对您有用。