Using custom Loopback user model with loopback-component-passport

后端 未结 1 861
清歌不尽
清歌不尽 2021-01-06 13:00

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

相关标签:
1条回答
  • 2021-01-06 13:18

    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.

    0 讨论(0)
提交回复
热议问题