我正在使用护照来通过Facebook验证我的应用程序的用户身份。 在我的应用程序中,我有2个不同的用户:客户和专业人员(转换为“专家”)。
我遵循Loopback docs来实现loopback-component-passport软件包和passport-facebook,并使其对我的客户有效。
因为我使用自定义用户模型,所以我遵循了SO Post的方法来修复该问题(注意:我仍然使用内置的AccessToken模型)。
我使用Angular前端。为了将用户重定向回我的前端,我创建了一个如下所示的启动脚本:
'use strict';
module.exports = function(app) {
var router = app.loopback.Router();
var cookieParser = require('cookie');
// Redirect user to page set in env.APP_HOST
router.get('/auth/customer/success', function(req, res, next) {
const cookie = req.headers.cookie;
var result = cookieParser.parse(cookie);
res.redirect(process.env.APP_HOST + '/auth/facebook/?token=' +
result['access_token'] + '&id=' + result['userId'] +
'&err=&fbsr=');
});
router.get('/auth/expert/success', function(req, res, next) {
const cookie = req.headers.cookie;
var result = cookieParser.parse(cookie);
res.redirect(process.env.APP_HOST + '/expert/auth/facebook/?token='
+ result['access_token'] + '&id=' + result['userId'] +
'&err=&fbsr=');
});
app.use(router);
};
我通过客户身份验证后,应重定向到https://example.com/auth/customer/success 当我通过专家/专家身份验证时,应重定向到https://example.com/auth/expert/success。
我必须同时在server.js中为客户和专业/专家加载配置。我创建了2个provider.json文件( providers.customer.json 和 provider.professional.json ),其中包含它们的特定信息。
这是我加载这两种配置的方式(可能有点混乱):
var passportCustomer = require('loopback-component-passport');
var PassportConfiguratorCustomer = passportCustomer.PassportConfigurator;
var passportConfiguratorCustomer = new PassportConfiguratorCustomer(app);
var passportProfessional = require('loopback-component-passport');
var PassportConfiguratorProfessional = passportProfessional.PassportConfigurator;
var passportConfiguratorProfessional = new PassportConfiguratorProfessional(app);
// Setup passport configuration
var passportCustomerConfig = {};
var passportProfessionalConfig = {};
try {
passportCustomerConfig = require('./providers.customer.json');
passportProfessionalConfig = require('./providers.professional.json');
} catch(err) {
console.log('Please configure the passport strategy in providers.customer.json');
console.error(err);
process.exit(1); // Fatal error
}
// Init passport for customer
passportConfiguratorCustomer.init(false);
passportConfiguratorProfessional.init(false);
// Setup passport models
passportConfiguratorCustomer.setupModels({
userModel: app.models.Customer,
userIdentityModel: app.models.UserIdentityCustomer,
userCredentialModel: app.models.UserCredentialsCustomer
});
passportConfiguratorProfessional.setupModels({
userModel: app.models.Professional,
userIdentityModel: app.models.UserIdentityProfessional,
userCredentialModel: app.models.UserCredentialsProfessional
});
// Configure passport for customer
for(var s in passportCustomerConfig) {
var c = passportCustomerConfig[s];
c.session = c.session !== false;
passportConfiguratorCustomer.configureProvider(s, c);
}
// Configure passport for professional/expert
for(var s in passportProfessionalConfig) {
var c = passportProfessionalConfig[s];
c.session = c.session !== false;
// passportConfiguratorProfessional.configureProvider(s, c);
passportConfiguratorCustomer.configureProvider(s, c);
}
我在Facebook中有2个不同的应用程序(1个用于客户,1个用于专业人士/专家)。当我使用localhost:3000 / auth / customer / facebook或localhost:3000 / auth / expert / facebook进行身份验证时,我看到两个应用程序都用于正确的端点。但是,无论我使用什么端点,在身份验证之后,我总是被重定向到http://example.com/expert/auth/facebook
所以我的问题是:如何解决此问题,以便将客户重定向到客户端点,并将专家/专业人士重定向到他们的专家端点?
供参考:providers.customer.json
{
"facebook-login": {
"provider": "facebook",
"module": "passport-facebook",
"clientID": "OhOh, I removed it :)",
"clientSecret": "Supa Dupa secret",
"callbackURL": "/auth/customer/facebook/callback",
"authPath": "/auth/customer/facebook",
"callbackPath": "/auth/customer/facebook/callback",
"successRedirect": "/auth/customer/success",
"failureRedirect": "/auth/customer/failure",
"scope": ["email"],
"failureFlash": true,
"profileFields" : ["locale", "name", "email"]
},
"facebook-link": {
"provider": "facebook",
"module": "passport-facebook",
"clientID": "OhOh, I removed it :)",
"clientSecret": "Supa Dupa secret",
"callbackURL": "/link/customer/facebook/callback",
"authPath": "/link/customer/facebook",
"callbackPath": "/link/customer/facebook/callback",
"successRedirect": "/auth/customer/success",
"failureRedirect": "/auth/customer/failure",
"scope": ["email"],
"link": true,
"failureFlash": true
}
}
供参考:providers.professional.json
{
"facebook-login": {
"provider": "facebook",
"module": "passport-facebook",
"clientID": "Aaaaand",
"clientSecret": "It's gone",
"callbackURL": "/auth/expert/facebook/callback",
"authPath": "/auth/expert/facebook",
"callbackPath": "/auth/expert/facebook/callback",
"successRedirect": "/auth/expert/success",
"failureRedirect": "/auth/expert/failure",
"scope": ["email"],
"failureFlash": true,
"profileFields" : ["locale", "name", "email"]
},
"facebook-link": {
"provider": "facebook",
"module": "passport-facebook",
"clientID": "Aaaaand",
"clientSecret": "It's gone",
"callbackURL": "/link/expert/facebook/callback",
"authPath": "/link/expert/facebook",
"callbackPath": "/link/expert/facebook/callback",
"successRedirect": "/auth/expert/success",
"failureRedirect": "/auth/expert/failure",
"scope": ["email"],
"link": true,
"failureFlash": true
}
}
答案 0 :(得分:1)
因此,我能够使用PassPortConfigurator#configureProvider
方法来解决此问题。我没有使用JSON文件来设置facebook身份验证,而是在JS中完成的。
var passportCustomer = require('loopback-component-passport');
var PassportConfiguratorCustomer = passportCustomer.PassportConfigurator;
var passportConfiguratorCustomer = new PassportConfiguratorCustomer(app); // Note the first letter is not a capital one
// Init passport for customer
passportConfiguratorCustomer.init(false);
// Setup passport models
passportConfiguratorCustomer.setupModels({
userModel: app.models.Customer,
userIdentityModel: app.models.UserIdentityCustomer,
userCredentialModel: app.models.UserCredentialsCustomer
});
// Load configuration here instead of the JSON file.
passportConfiguratorProfessional.configureProvider('facebook-login-expert', {
module: 'passport-facebook',
clientID: XXX,
clientSecret: XXX,
callbackURL: '/auth/expert/facebook/callback',
authPath: '/auth/expert/facebook',
callbackPath: '/auth/expert/facebook/callback',
successRedirect: '/auth/expert/success',
failureRedirect: '/auth/expert/failure',
scope: ['email'],
failureFlash: true,
profileFields: ['locale', 'name', 'email']
});