I am building a login registration. I have used passport before and was able to have it working. It seems like npm has changed the documentation. I cannot even console.log in the function I pass passport in. I have been working and researching this issue since last night. 

Currently I am able to register a user, and authenticate a user which means my register and authenticate route is working. This is verified through Postman. When I use the profile route though it is unauthorized. I will put what I am putting through postman below after I describe the file structure and past the code for each file. 

If you notice in the passport file, I have a console.log. This does not even log while my console.log in the app.js is logging in the terminal. 
So this is all that shows in my terminal 

Server started on port 3000
yay i am connected to databasemongodb://localhost:27017/authapp

Could someone please help? 

Here is my file structure. 

```
application
config
-database.js
-passport.js
models
-user.js
routes
-users.js
app.js
package.json

```

passport.js
-----------

```
   module.exports = function(passport){
    let opts = {}; 
    opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt')  
    opts.secretOrKey = config.secret; 
    passport.use(new JwtStrategy(opts, (jwt_payload,done)=>{
        console.log(jwt_payload);  
        User.getUserById(jwt_payload._doc._id, (err, user)=>{  
            if(err){
                return done(err,false);
            }
            if(user){
                return done(null, user);
            }else{
                return done(null,false);
            }
        });
    }));
}

```

database.js
-----------

```
module.exports = {
    database:'mongodb://localhost:27017/authapp', 
    secret:'NintamaRantaro'
}

```

models/user.js
--------------

```
const mongoose = require('mongoose');
//bcrpypt for encrpyption
const bcrypt = require('bcryptjs');
//to connect to database 
const config = require('../config/database');

//Create the Schema
const UserSchema = mongoose.Schema({
    name: {
        type: String
    },
    email: {
        type: String,
        require: true,

    },
    username: {
        type: String,
        require: true
    },
    password: {
        type: String,
        require: true
    }
});

const User = module.exports = mongoose.model('User', UserSchema);

module.exports.getUserById = function(id, callback){
    User.findById(id, callback);
    console.log("got user by id")
}

module.exports.getUserByUsername = function(username, callback){
    const query = {username:username} 
    User.findOne(query, callback); 
}

module.exports.addUser = function(newUser, callback){ /
  bcrypt.genSalt(10, (err, salt) => {
    bcrypt.hash(newUser.password, salt, (err, hash) => {
      if(err) throw err;
      newUser.password = hash;
      newUser.save(callback);
      console.log("new user has been added")
    });
  });
}

module.exports.comparePassword = function(candidatePassword, hash, callback){
    bcrypt.compare(candidatePassword, hash, function(err, isMatch){
        if(err) throw err;
        callback(null, isMatch);
        console.log("compare pass complete")
    });
}

```

routes/users.js
---------------

```
const express = require('express');
const router = express.Router();
const passport = require('passport');
const jwt = require('jsonwebtoken');
const config = require('../config/database')
//Now that I created the model I will bring it in here.
const User = require('../models/user');
const bodyParser = require('body-parser')

//Registration 
router.post('/register', (req,res,next) =>{
    //res.send('registration');
    let newUser = new User({
        name: req.body.name,
        email: req.body.email,
        username: req.body.username,
        password: req.body.password  //I will run this password through bcrypt.hash which will has before db.
    });
    console.log("new instance of the User class has been created")
    User.addUser(newUser, function(err, user){ //I will create this addUser function inside the models user.js
        if(err){
            console.log(err);
            res.json({success:false, msg:'Registration Failed!'})
        }else{
            res.json({success:true, msg:'User is Registered!'})
        }
    });
});
//This will be my authentication route
router.post('/authenticate', (req,res,next)=>{
    const username = req.body.username;
    const password = req.body.password;

    User.getUserByUsername(username, (err, user)=>{
        if(err) throw err;
        if(!user){
            return res.json({success: false, msg:'user not found'})
        }
        User.comparePassword(password, user.password, (err, isMatch)=>{
            if(err) throw err;
            if(isMatch){
                const token = jwt.sign(user.toJSON(), config.secret, {
                    expiresIn:600000
                });
                res.json({
                    sucess:true,
                    token:'JWT ' + token,
                    user:{
                        id: user._id,
                        name: user.name,
                        username: user.username,
                        email: user.email
                    }
                });
            }else{
                return res.json({success:false, msg:'wrong pass'});
            }
        });
     });
});
// It failed at the line.
// const token = jwt.sign(user, config.secret, {
// Which I assume is mongoosejs object, which contains many methods and is not "serializable". 

router.get('/profile', passport.authenticate('jwt', {session:false}), (req, res, next) => {
  console.log(req.user)
  res.json({user: req.user});

});

module.exports = router;

```

app.js
------

```
const express = require('express');
//path is part of the cores module
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const mongoose = require('mongoose');
//database is in database.js this connects to  database:'mongodb://localhost:27817/authapp'
const config = require('./config/database')

```

mongoose.connect(config.database);

```
mongoose.connect(config.database);  

mongoose.connection.on('connected',function(){console.log('yay i am connected to database'+config.database)});

mongoose.connection.on('error',function(error){console.log('You have an error'+error)});

const app = express();

const users = require('./routes/users');

const port = 3000;

app.use(cors());

app.use(express.static(path.join(__dirname, 'public')))

app.get('/', function(req,res){res.send('Sending Response')})

app.use(bodyParser.json());

app.use(passport.initialize());
app.use(passport.session());

require('./config/passport')(passport);

app.use('/users', users)

app.listen(port, function(){console.log('Server started on port '+port)})

```

Postman after <http://localhost:3000/users/register>
method:Post 
Body: 

```
{
    "name":"hello",
    "email":"hello@world.com",
    "username":"helloworld",
    "password":"123456"
}

```

200 OK
 {
 "success": true,
 "msg": "User is Registered!"
 }

After <http://localhost:3000/users/authenticate>
method:Post
body:

```
{
    "username":"helloworld",
    "password":"123456"
}

```

200 OK

```
{
    "sucess": true,
    "token": "JWTeyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTk2YzA1ZmZjNDQ5YjBkZTI0ZTA3YTIiLCJuYW1lIjoiaGVsbG8iLCJlbWFpbCI6ImhlbGxvQHdvcmxkLmNvbSIsInVzZXJuYW1lIjoiaGVsbG93b3JsZCIsInBhc3N3b3JkIjoiJDJhJDEwJGl1eFE2V1IvaXJqRkxTZVV4MkhSVE80SlhzeEhrUklzbEhGeTVGL1ZQbGdSMVBEU2wwUkRlIiwiX192IjowLCJpYXQiOjE1MTk4MjkxMTksImV4cCI6MTUyMDQyOTExOX0.05uAxA9sQMzVHjc2kXoR86fpDzu1TQmsyFbGN_AcFRo",
    "user": {
        "id": "5a96c05ffc449b0de24e07a2",
        "name": "hello",
        "username": "helloworld",
        "email": "hello@world.com"
    }
}

```

After <http://localhost:3000/users/profile>

Headers:

```
Key: Authorization,
Value: JWTeyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTk2YzA1ZmZjNDQ5YjBkZTI0ZTA3YTIiLCJuYW1lIjoiaGVsbG8iLCJlbWFpbCI6ImhlbGxvQHdvcmxkLmNvbSIsInVzZXJuYW1lIjoiaGVsbG93b3JsZCIsInBhc3N3b3JkIjoiJDJhJDEwJGl1eFE2V1IvaXJqRkxTZVV4MkhSVE80SlhzeEhrUklzbEhGeTVGL1ZQbGdSMVBEU2wwUkRlIiwiX192IjowLCJpYXQiOjE1MTk4MjkxMTksImV4cCI6MTUyMDQyOTExOX0.05uAxA9sQMzVHjc2kXoR86fpDzu1TQmsyFbGN_AcFRo

```

Unauthorized 
401 Unauthorized