app.use(session()) gives err is not defined

I am completely new to node js. This am doing for learning purpose. I am using passport js to validate username and login it works fine.

passport.use(new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password'
}, function (username, password, done) {
console.log(username,password);
User1.collection('mydb1', function(err, collection) {
    if (!err) {
        collection.findOne({
            username:username
        }, function(err,user) {
            console.log(user);
            if (err) {
                return done(err);
            }

            if (!user) {
                return done(null, false, { message: 'Incorrect username.' });
            }

            if (user.password != password) {
                return done(null, false, {
                    message: 'Invalid password' 
                });
            } else {
                return done(null, user);
            }
     })
}});

configuration

app.use(bodyParser.urlencoded({extended: true}));
app.use(passport.initialize());
app.use(passport.session());

Authentication

app.post(
    '/',
    passport.authenticate(
        'local',
        {
            successRedirect: '/success',
            failureRedirect: '/loginerror'
        }
    )
);

in /success i want to display the username in another page i try to access username like request.user.usernmame

app.get('/success', function(request, response){
    response.send('ok');
    console.log(request.user.username);
});

But i got the following Error message in console

TypeError: Cannot read property 'usernmame' of undefined
at Object.app.post.passport.authenticate.successRedirect [as handle] (D:\nod
ejsmodule\mongodb\server.js:114:25)
at next_layer (D:\nodejsmodule\node_modules\express\lib\router\route.js:103:13)
 at Route.dispatch (D:\nodejsmodule\node_modules\express\lib\router\route.js:107:5)
 at D:\nodejsmodule\node_modules\express\lib\router\index.js:213:24
 at Function.proto.process_params (D:\nodejsmodule\node_modules\express\lib\r
 outer\index.js:286:12)
 at next (D:\nodejsmodule\node_modules\express\lib\router\index.js:207:19)
 at SessionStrategy.strategy.pass (D:\nodejsmodule\node_modules\passport\lib\
 middleware\authenticate.js:314:9)
 at SessionStrategy.authenticate (D:\nodejsmodule\node_modules\passport\lib\s
 trategies\session.js:67:10)
 at attempt (D:\nodejsmodule\node_modules\passport\lib\middleware\authenticat
 e.js:337:16)
 at Layer.authenticate [as handle] (D:\nodejsmodule\node_modules\passport\lib
 \middleware\authenticate.js:338:7)

if i add app.use(session({ secret: 'anything',saveUninitialized: true,resave: true })); in configuration part i got following Error message in console

ReferenceError: err is not defined
at passport.use.LocalStrategy.usernameField (D:\nodejsmodule\mongodb\server.
js:67:10)
at pass (D:\nodejsmodule\node_modules\passport\lib\authenticator.js:353:9)
at Authenticator.deserializeUser (D:\nodejsmodule\node_modules\passport\lib\
authenticator.js:358:5)
at SessionStrategy.authenticate (D:\nodejsmodule\node_modules\passport\lib\s
trategies\session.js:49:28)
at attempt (D:\nodejsmodule\node_modules\passport\lib\middleware\authenticat
e.js:337:16)
at Layer.authenticate [as handle] (D:\nodejsmodule\node_modules\passport\lib
\middleware\authenticate.js:338:7)
at trim_prefix (D:\nodejsmodule\node_modules\express\lib\router\index.js:254
:17)
at D:\nodejsmodule\node_modules\express\lib\router\index.js:216:9
at Function.proto.process_params (D:\nodejsmodule\node_modules\express\lib\r
outer\index.js:286:12)
at next (D:\nodejsmodule\node_modules\express\lib\router\index.js:207:19)

I have the same problem before. I think, that you have the same too.

The reason for it, that Passport can't get data from POST body. For fix it, you must provide req and res variables of Express to Passport.

Try wrap your code with auhtentication with (req, res):

app.post(
    '/',
    passport.authenticate('local', {
        successRedirect: '/success',
        failureRedirect: '/loginerror'
    })(req, res)
);

For better vision I will write it like this:

app.post('/', passport.authenticate('local')(req, res));

Try to do console.log('myreq',request); and after try to change from app.get to app.post. sorry im with Mobile versión.

Something like:

app.post('/success', function(request, response){ response.send('ok'); console.log('myreq',request); console.log(request.user.username); });

Try to do console.log('myreq',request); and after try to change from app.get to app.post. sorry im with Mobile versión.