2015-12-31 141 views
1

我開始以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" 
    } 

回答

2

根據在這裏提供的文檔 -

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

你只需要安裝回送護照NPM模塊和下面的東西添加到您的項目 -

1)providers.json - 這將定義第三方認證提供者的配置你w螞蟻使用像Facebook,谷歌加等 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); 
} 

此之後,你可以檢查出提供讓不同的客戶端軟件開發工具包請求登錄或社交認證,並將其用於登錄和註冊用戶。

相關問題