Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"unknown authentication strategy" "jwt"

Tags:

node.js

jwt

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

like image 776
Yves Avatar asked Nov 07 '22 23:11

Yves


1 Answers

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);
like image 64
Furkan Ozdemir Avatar answered Nov 15 '22 06:11

Furkan Ozdemir