5

I'm now retired and now have some time to learn how to develop an MEVN application ;-) I'm now in the point of securing a few pages in the app with a jwt token. I can log a user and later read its token:

app.get('/movies', (req,res) => {
    jwtOptions.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt');
     console.log(Version + 'JWT token: ' + jwtOptions.jwtFromRequest(req));
    Movie.find({}, 'name description release_year genre', (error, movies) => {
        if(error) { console.log(error);}
        console.log(Version + "Fetched " + movies.length + " movies");
        res.send(movies);
    });
});

I get the user token in the console. Fine! This get request is not yet protected. To protect it I modify slightly the 1st line:

app.get('/movies', passport.authenticate('jwt', { session: false }), (req,res) => {
...

An error is now triggered.

Unknown authentication strategy "jwt"

After reading a few million questions and answers on various sites, I'm short of any idea to fix my problem. Suggestions will be highly appreciated.

Thanks

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
Yves
  • 51
  • 1
  • 4

1 Answers1

1

Did you check your server.js or index.js? you should add some initializer. I received same error and I was missed those

const passport = require('passport');
// Passport middleware
app.use(passport.initialize());

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