最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set the current host for Passport strategy callbackURL? - Stack Overflow

programmeradmin0浏览0评论

I'm using the Passport-Linkedin strategy for Passport with Express, to allow users to log in with their LinkedIn profile.

I have the following code:

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

On line 4, I have to set the full callback URL manually. I had one string for production and one for development, but my URLs keep changing, and so do the ports (I use 2 machines to develop).

How can I set the first portion of the URL (http://localhost:3000) automatically? Is there a property of express or app that would allow me to do that? Do I need to resort to an app.use(function(req, res){});?

Thanks!

I'm using the Passport-Linkedin strategy for Passport with Express, to allow users to log in with their LinkedIn profile.

I have the following code:

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

On line 4, I have to set the full callback URL manually. I had one string for production and one for development, but my URLs keep changing, and so do the ports (I use 2 machines to develop).

How can I set the first portion of the URL (http://localhost:3000) automatically? Is there a property of express or app that would allow me to do that? Do I need to resort to an app.use(function(req, res){});?

Thanks!

Share Improve this question edited Jan 6, 2015 at 22:48 laggingreflex 34.7k36 gold badges143 silver badges200 bronze badges asked Oct 13, 2012 at 2:49 Traveling Tech GuyTraveling Tech Guy 27.8k25 gold badges116 silver badges166 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

Old question, possibly answer only valid for newer versions. However if someone runs into this, like me, solution is just to not specify a hostname in the callbackURL:

passport.use(new LinkedInStrategy({
    consumerKey: config.linkedin.LINKEDIN_API_KEY,
    consumerSecret: config.linkedin.LINKEDIN_SECRET_KEY,
    callbackURL: "/auth/linkedin/callback"
  },

To get this to work for heroku https redirect, we have to tell the system to trust the x-forwarded-protocol headers, by trusting the proxy:

passport.use(new LinkedInStrategy({
    consumerKey: config.linkedin.LINKEDIN_API_KEY,
    consumerSecret: config.linkedin.LINKEDIN_SECRET_KEY,
    callbackURL: "/auth/linkedin/callback",
    proxy: true
  },

Eventually solved this by dynamically building the callback URL from pieces of URL and the actual port. Not happy with this solution as it looks non-elegant, but could not find a way to get the actual URL without adding a middleware use call (which I'm sure impacts performance more than a simple string concatenation).

In my config.js I have a cfg.site_url, that's one way, or you could look at req.host

http://expressjs./api.html#req.host

// Host: "example.:3000"
req.hostname
// => "example."

Not sure if you have a req object in your context there.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>