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

passReqToCallback seems to use a cached version of request... #283

Closed
gad2103 opened this issue Sep 8, 2014 · 1 comment
Closed

passReqToCallback seems to use a cached version of request... #283

gad2103 opened this issue Sep 8, 2014 · 1 comment

Comments

@gad2103
Copy link

gad2103 commented Sep 8, 2014

hi there. thanks for this awesome library.

i'm having some trouble getting something to work. i'm using a token strategy not sessions and i'm trying to have one handler for both authentication and authorization (just like at the end of the Authorize example in the docs http://passportjs.org/guide/authorize/)

i have some middleware that is called before the call to passport authenticate like so:

'use strict';

var express = require('express');
var passport = require('passport');
var auth = require('../auth.service');

var router = express.Router();

router
  .get('/', auth.bindUserToReq(), auth.testU2Q(), passport.authenticate('facebook', {
    scope: ['email', 'user_about_me', 'read_stream', 'publish_actions', 'user_likes'],
    failureRedirect: '/signup',
    session: false
  }))

  .get('/callback', passport.authenticate('facebook', {
    failureRedirect: '/signup',
    session: false
  }), auth.setTokenCookie);

module.exports = router;

the purpose of that middleware is to add user to the req object, so that i can check for it in passport just like the example suggests. in my passport file i have the following at the beginning:

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

exports.setup = function (User, config) {

  passport.use(new FacebookStrategy(config.facebook,
    function(req, accessToken, refreshToken, profile, done) {
        console.log(req.user);
        process.nextTick(function() {
        if (!req.user) {
          User.findOne({'strategies.facebook.id': profile.id}, function(err,user){

my config has the proper passReqToCallback setting (true) to make this supposedly happen correctly. however, any modifications i make to the request object before calling passport in the route get completely ignored.

please let me know if i'm doing something totally stupid or if this is a bug. thanks!

@gad2103
Copy link
Author

gad2103 commented Sep 8, 2014

UPDATE:
for anyone having this issue and possibly, like me, using the yeoman fullstack generator, i was (stupidly) putting the middleware call in the wrong route. here is the working solution:

'use strict';

var express = require('express');
var passport = require('passport');
var auth = require('../auth.service');

var router = express.Router();

router
  .get('/', passport.authenticate('facebook', {
    scope: ['email', 'user_about_me', 'read_stream', 'publish_actions', 'user_likes'],
    failureRedirect: '/signup',
    session: false
  }))

  .get('/callback',  auth.bindUserToReq(), auth.testU2Q(), passport.authenticate('facebook', {
    failureRedirect: '/signup',
    session: false
  }), auth.setTokenCookie);

module.exports = router;
``

@gad2103 gad2103 closed this as completed Sep 8, 2014
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