Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirection fails when splitting in different files #690

Open
AndresMarotta opened this issue Aug 20, 2018 · 0 comments
Open

Redirection fails when splitting in different files #690

AndresMarotta opened this issue Aug 20, 2018 · 0 comments

Comments

@AndresMarotta
Copy link

AndresMarotta commented Aug 20, 2018

I'm using passport.js authentication for different API's and works fine, but I want to separate the code for each one and I'm stuck calling passport.authenticate('facebook'). I'll put Facebook code for example.

This works:

const passport = require('passport');
const FacebookStrategy = require('passport-facebook').Strategy;
const fb = require('fb');

passport.use(new FacebookStrategy({
  clientID: 'XXXXXXXXXXXXXXXX',
  clientSecret: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  callbackURL: "https://127.0.0.1/facebook/authCallback/"
}, function(accessToken, refreshToken, profile, cb) {
  return {accessToken, refreshToken, profile, cb};
}));

// Entry point
app.get('/authorize/:clientId/:network', passport.authenticate('facebook'));

app.get('/:network/authCallback', (request, response) => {
  // Some code here...
});

But when I try to split it in a different file, it never callback from passport.authenticate('facebook'):

sserve.js:

const authUtils = require('./utils/authUtils');

app.get('/authorize/:clientId/:network', (request, response) => {
  authUtils.authorize(request.params.clientId, request.params.network);
});

authUtils.js:

async function authorize(clientId, network) {    
  try {
    config = await databaseUtils.getConfig(clientId, network);

    if(typeof(config) !== 'undefined') {
      passport.use(new FacebookStrategy({
        clientID: config.appId,
        clientSecret: config.appSecretKey,
        callbackURL: 'https://127.0.0.1/' + network + '/authCallback/'
      }, function(accessToken, refreshToken, profile, cb) {
        return {accessToken, refreshToken, profile, cb};
      }));

      // It reachs to this point but never comeback...
      passport.authenticate(network);
    } else {
      throw new Error(network + ' is not configured');
    }
  } catch (e) {
    throw e;
  }
}

After some debug test, the problem seems to be that passport.authenticate(network) doesn't make redirection to callback URL.

If I add:

app.get('/authorize/:clientId/:network', (request, response) => {
  authUtils.authorize(request.params.clientId, request.params.network);
  console.log(1);
  next();
});

Console shows "1" and browser return 404 Not Found. Passport doesn't continue with the redirection process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant