通过loopback-passport进行社交登录身份验证

时间:2015-12-31 17:33:41

标签: node.js loopbackjs strongloop

我开始以loopback api获得理由。目前我正在尝试通过社交登录为我的应用程序集成身份验证。我找到了三个页面,表明如何完成这一点,但他们都表明这有点不同而且不清楚:github-loopback-component-passport& npmjs-loopback-component-passport& github-loopback-component-passport-example。关于什么是最新的流程,我有点困惑。任何人都可以阐明如何最好地将社交登录与环回整合?另外,如何测试可能需要访问令牌的路线?这是项目的Github Repo

当前依赖关系

"dependencies": {
    "compression": "^1.0.3",
    "cors": "^2.5.2",
    "lodash": "^3.10.1",
    "loopback": "^2.26.2",
    "loopback-boot": "^2.6.5",
    "loopback-component-explorer": "^2.1.0",
    "loopback-connector-mysql": "^2.2.0",
    "loopback-datasource-juggler": "^2.19.0",
    "loopback-stormpath": "0.0.1",
    "serve-favicon": "^2.0.1",
    "passport": "^0.3.2",
    "underscore": "^1.8.2"
  }

1 个答案:

答案 0 :(得分:2)

根据此处提供的文件 -

https://www.npmjs.com/package/loopback-passport

您只需要安装loopback-passport npm模块并将以下内容添加到您的项目中 -

1)providers.json - 这将定义您想要使用的第三方身份验证提供商的配置,如facebook,google plus等。 2)在你的server.js中定义护照默认配置,如下所示链接 -

var loopback = require('loopback');
var path = require('path');
var app = module.exports = loopback();

// Create an instance of PassportConfigurator with the app instance 
var PassportConfigurator = require('loopback-passport').PassportConfigurator;
var passportConfigurator = new PassportConfigurator(app);

app.boot(__dirname);

...

// Enable http session 
app.use(loopback.session({ secret: 'keyboard cat' }));

// Load the provider configurations 
var config = {};
try {
  config = require('./providers.json');
} catch(err) {
  console.error('Please configure your passport strategy in `providers.json`.');
  console.error('Copy `providers.json.template` to `providers.json` and replace the clientID/clientSecret values with your own.');
  process.exit(1);
}

// Initialize passport 
passportConfigurator.init();

// Set up related models 
passportConfigurator.setupModels({
  userModel: app.models.user,
  userIdentityModel: app.models.userIdentity,
  userCredentialModel: app.models.userCredential
});

// Configure passport strategies for third party auth providers 
for(var s in config) {
  var c = config[s];
  c.session = c.session !== false;
  passportConfigurator.configureProvider(s, c);
}

在此之后,您可以查看提供的不同客户端sdks以发出登录或社交身份验证请求,并使用它们来记录和注册用户。