将自定义回送用户模型与回送组件密码一起使用 [英] Using custom Loopback user model with loopback-component-passport

查看:73
本文介绍了将自定义回送用户模型与回送组件密码一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将扩展的回送用户模型与loopback-component-passport一起用于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": {}
  }
}

如您所见,用户"中填充了我新创建的用户,我的用户"为空.

我误会什么,还是将回传用户与护照一起扩展的正确方法是什么? 任何提示或对示例的引用都将不胜感激!

解决方案

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

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)
}

还有示例存储库,这可能对您有用. /p>

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!

解决方案

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.

这篇关于将自定义回送用户模型与回送组件密码一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆