Reputation:
So I have a small app based on the generator-angular-fullstack. I want to add to the current logged user the credential for a new account (Withings). The idea is that a user that is logged in can also add other way to make the login by using Withings or Twitter or Facebook.
Those are my route:
router
.get('/', passport.authorize('withings', {
failureRedirect: '/'
}))
.get('/callback', passport.authorize('withings', {
successRedirect : '/settings',
failureRedirect : '/'
}));
and this the callbacks implementation :
passport.use(new WithingsStrategy({
consumerKey: config.withings.clientID,
consumerSecret: config.withings.clientSecret,
callbackURL: config.withings.callbackURL,
passReqToCallback: true
},
function(req, token, tokenSecret, profile, done) {
console.log('user' + req.user);
return done(null, null);
}
));
The point is that when I get back to the function, even if I was logged the req.user
is always undefined
.
Does anyone have an idea? I read that you need a couple of function like deserializeUser
and serializeUser
but they are never called.
Ideas? I'm new on this kind of things and after 3-4 night is getting frustrated :(
PS: this is my configuration
app.set('views', config.root + '/server/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(compression());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session()); //persistent login session
// Persist sessions with mongoStore
// We need to enable sessions for passport twitter because its an oauth 1.0 strategy
app.use(session({
secret: config.secrets.session,
resave: true,
saveUninitialized: true,
store: new mongoStore({ mongoose_connection: mongoose.connection })
}));
if ('production' === env) {
app.use(favicon(path.join(config.root, 'public', 'favicon.ico')));
app.use(express.static(path.join(config.root, 'public')));
app.set('appPath', config.root + '/public');
app.use(morgan('dev'));
}
if ('development' === env || 'test' === env) {
app.use(require('connect-livereload')());
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(path.join(config.root, 'client')));
app.set('appPath', 'client');
app.use(morgan('dev'));
app.use(errorHandler()); // Error handler - has to be last
}
Upvotes: 2
Views: 1652
Reputation: 441
I know this question is old, but in case anyone else comes past it, I see 2 problems.
First, in your callback implementation you say:
return done(null, null);
The API for done
is done([err], [user])
. Your code, by passing null
as the second argument to done, is saying you didn't find a user, thus passport is not logging a user in. You actually need to say:
return done(null, profile);
Second, you need to move these lines:
app.use(passport.initialize());
app.use(passport.session());
down below your app.use(session({...}))
call. Passport depends on the express session, so you need to setup the express session before initializing passport.
Hope this helps!
Upvotes: 2