freethway
freethway

Reputation: 26

How to pass a variable taken from route handler into passport-freshbooks strategy's middleware definition?

I'm using a passport-freshbooks strategy, which is like pretty much like any other strategy, except I think it's not authored properly (if I'm not missing anything). I've found that you need to pass a variable to its middleware in its definition phase, which you can only get from the user through a route handler.

passport.use(new FreshbooksStrategy({
    // This is the USER's subdomain, who's supposed to login
    subdomain: SUBDOMAIN,
    ...

To set the subdomain above, you need to first get it from the user

app.get('/login', function(req,res){
    res.send('<form>Enter your freshbooks account URL or subdomain(..submit)</form>')
});
app.post('/login', function(req,res){
    var subdomain = req.body.subdomain.split('.')[0].split('/').pop();
});

So how could I set this subdomain in the passport strategy's middleware definition above?

It might need to alter the strategy itself but I'm not sure how to proceed, any ideas?

issue on github

Upvotes: 0

Views: 119

Answers (2)

Wandering
Wandering

Reputation: 21

Passport has a "passReqToCallback" option that makes your request object accessible in your strategy. With this option enabled, req will be passed as the first argument to the verify callback.

Here it is being used in an example. (Gotten from their docs)

passport.use(new TwitterStrategy({
    consumerKey: TWITTER_CONSUMER_KEY,
    consumerSecret: TWITTER_CONSUMER_SECRET,
    callbackURL: "http://www.example.com/auth/twitter/callback",
    passReqToCallback: true
  },
  function(req, token, tokenSecret, profile, done) {
    if (!req.user) {
      // Not logged-in. Authenticate based on Twitter account.
    } else {
      // Logged in. Associate Twitter account with user.  Preserve the login
      // state by supplying the existing user after association.
      // return done(null, req.user);
    }
  }
));

Upvotes: 2

Todd R
Todd R

Reputation: 18516

I ended up hacking the library and had it working at one point. Ultimately, I dropped the project so i don't have any fixes to share. But if you're thinking of modifying the strategy, you're on the right path.

Upvotes: 0

Related Questions