我使用Passport with Express的Passport-Linkedin战略,允许用户使用其LinkedIn个人资料登录。如何设置Passport策略的当前主机策略callbackURL?
我有以下代码:
passport.use(new LinkedInStrategy({
consumerKey: config.linkedin.LINKEDIN_API_KEY,
consumerSecret: config.linkedin.LINKEDIN_SECRET_KEY,
callbackURL: "http://localhost:3000/auth/linkedin/callback"
},
function(token, tokenSecret, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function() {
// To keep the example simple, the user's LinkedIn profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the LinkedIn account with a user record in your database,
// and return that user instead.
return done(null, profile);
});
}
));
第4行中,我必须手动设置全回调URL。我有一个字符串用于生产,一个用于开发,但我的URL不断变化,端口也一样(我使用2台机器开发)。
如何自动设置URL的第一部分(http://localhost:3000
)?是否有express
或app
的财产可以让我这样做?我需要使用app.use(function(req, res){});
吗?
谢谢!
如果您的应用程序未在根路径上运行,则不起作用,例如,它在'/ myapp'而不是'/'上运行。 –