TSM
TSM

Reputation: 189

req.user is undefined

I want to write a REST-API with NodeJS+Restify and PassportJS for the authentication. When the user logs in, I generate a sessionId and store it in a session, which I create myself.

var Passport                = require( 'passport' );
var PassportBearerStrategy  = require('passport-http-bearer').Strategy;
// Authentication
Passport.use( new PassportBearerStrategy( function( sessionId, cb ) {
    Auth.getSession( sessionId, function( userId ){
        cb( ( !!userId ), { userId: userId }, userId );
    });
}));

server.get(
    '/user',
    Passport.authenticate( 'bearer' ),
    function( req, res, next ){
        console.info( req.user  ); // undefined
        respond( res, {} );
        return next();
    }
);

Everything works except, that in the function bellow, req.user is undefined. I read, that you have to enable session. But I already implemented my own session, I do not need another one.
All I need is that, if I say cb( true, user ) it arrives in the function below.

Is there any way to solve this?

Upvotes: 0

Views: 470

Answers (1)

ilmirons
ilmirons

Reputation: 664

As I get from the code in this example the callback cb follows the standard node format, so gets an error as first argument and the user as second. I think that callback is what set req.user and manage all post-auth boilerplate. You should try to call it as `cb(null, user)

Upvotes: 1

Related Questions