【问题标题】:Using custom Loopback user model with loopback-component-passport使用带有 loopback-component-passport 的自定义 Loopback 用户模型
【发布时间】:2017-07-20 14:10:50
【问题描述】:

我正在尝试将扩展的环回用户模型与环回组件护照一起用于 facebook 登录。登录本身正在工作,但我无法让它使用我的自定义用户模型而不是内置的“用户”。

我采取的步骤:

- 使用 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": {}
  }
}

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

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

【问题讨论】:

    标签: facebook express loopbackjs passport.js


    【解决方案1】:

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

    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,可能对你有用。

    【讨论】:

    • 非常感谢!我已经在质疑我的理智了:D 但老实说,整个用户在环回中扩展的东西不是最理想的......
    • 我同意这一点。我也花了一段时间。尽管查看他们的发行说明,但他们正在逐渐改进这些东西。让我们希望它会变得更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多