问题
I'm trying to use a extended loopback user model together with the loopback-component-passport for facebook login. The login itself is working but i can't get it to use my custom user model instead of the builtin "Users".
steps i took:
- create custom user model with 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": {}
}
- Setup passport component with the new user model:
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']);
};
Problem: When i log in with via facebook the passport component still uses the "Users" model as seen in my db.json storage:
{
"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": {}
}
}
As you can see "Users" is populated with my newly created user and "myuser" is empty.
Am i mistaking something or what is the correct way to extend the loopback user together with passport? Any tips or references to a example are greatly appreciated!
回答1:
You will have to extend all passport related models, so you can have them linked to your custom user model.
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 (relevant part)
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)
}
There is also an example repository, which might be useful for you.
来源:https://stackoverflow.com/questions/42528518/using-custom-loopback-user-model-with-loopback-component-passport