gpt4 book ai didi

facebook - 将自定义 Loopback 用户模型与 loopback-component-passport 结合使用

转载 作者:行者123 更新时间:2023-12-01 04:35:34 26 4
gpt4 key购买 nike

我正在尝试将扩展的环回用户模型与 loopback-component-passport 一起用于 facebook 登录。登录本身是有效的,但我无法让它使用我的自定义用户模型而不是内置的“用户”。

我采取的步骤:

- 使用 slc loopback:model extending "User"创建自定义用户模型

{
"name": "myuser",
"plural": "myusers",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"mytestproperty": {
"type": "string",
"default": "myteststring"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}

- 使用新用户模型设置 Passport 组件:

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 登录时, Passport 组件仍然使用我的 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": {}
}
}

如您所见,“用户”中填充了我新创建的用户,而“我的用户”为空。

我是不是弄错了什么,或者将环回用户与 Passport 一起扩展的正确方法是什么?非常感谢任何提示或对示例的引用!

最佳答案

您必须扩展所有与 Passport 相关的模型,以便将它们链接到您的自定义用户模型。

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": []
}

user-identity.json

{
"name": "userIdentity",
"plural": "userIdentities",
"base": "UserIdentity",
"properties": {},
"validations": [],
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
},
"acls": [],
"methods": []
}

user-credential.json

{
"name": "userCredential",
"plural": "userCredentials",
"base": "UserCredential",
"properties": {},
"validations": [],
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
},
"acls": [],
"methods": []
}

access-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 ,这可能对您有用。

关于facebook - 将自定义 Loopback 用户模型与 loopback-component-passport 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42528518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com