2017-03-01 20 views
2

我想使用一個擴展的回送用戶模型以及用於Facebook登錄的loopback-component-passport。登錄本身正在工作,但我不能讓它使用我的自定義用戶模型,而不是內置的「用戶」。使用自定義Loopback用戶模型與迴環組件護照

步驟我把:

- 創建SLC迴環自定義用戶模式:延長 「用戶」

{ 
    "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「是空的。

我錯了什麼東西或什麼是正確的方式來擴展回送用戶和護照一起嗎? 任何提示或引用的例子,非常感謝!

回答

3

您必須擴展所有護照相關模型,以便您可以將它們鏈接到您的自定義用戶模型。

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,這可能是對你有用。

+1

非常感謝!我已經在質疑我的理智了:D 但說實話,整個用戶在環回中延伸的東西是不理想的... –

+0

我同意這一點。這也花了我一段時間。雖然看他們的發行說明,他們正在逐漸改進這些東西。讓我們希望它會變得更好。 –