in Education by
const JwtStrategy = require('passport-jwt').Strategy, ExtractJwt = require('passport-jwt').ExtractJwt; const User = require('../models/User'); const key = require('./keys').secret; const mongoose = require('mongoose'); var opts = {} opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken(); opts.secretOrKey = key; module.exports = (passport) => { passport.use(new JwtStrategy(opts, function(jwt_payload, done) { console.log(jwt_payload); User.findById(jwt_payload.id, function(err, user) { if (err) { return done(err, false); } if (user) { return done(null, user); } else { return done(null, false); } }); })) } I am using passport-jwt stratergy to validate with token but this is file is not running properly i even tried to console the payload here but this is not even logging it passport.use(new JwtStrategy(opts, function(jwt_payload, done) console.log(jwt_payload); Token Stratergy const payload = { id: user.id, username: user.username, name: user.name } const token = jwt.sign(payload, config.secret, { expiresIn: 36000 }); return res.json({ success: true, token: 'JWT ' + token, user: { id: user.id, name: user.name, username: user.username } }) And when i am using this on the protected routes it is not authorizing the protected routes Result = Unauthorized router.get('/profile', passport.authenticate('jwt', { session: false }), (req, res) => { res.send("Profile"); }); JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The reason your authentication is not working is that you've used the legacy token prefix "JWT" and a modern JWT (fromAuthHeaderAsBearerToken) extractor as an option to your strategy. You can do one of the steps to fix the problem: change the token prefix to 'Bearer ' + token change the token extractor: opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme("JWT");

Related questions

0 votes
    I am trying to write some stuff in a file using reactJS and nodeJs but it doesnt work. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to write some stuff in a file using reactJS and nodeJs but it doesnt work. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I am running my nodejs app by npm start I just installed nodemon by sudo npm install -g nodemon ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I'm trying to include actual date and time to the certain message, however I'm unable to do it, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    axios.js throws CONNRESET error (certificate not found in request) but request package works for same p12 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    so i'm reviewing and practicing making rest api with node mongoose and express. I'm having problem making ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I'm trying to debug my nodejs app using node-inspector. But Google Chrome doesn't show the code. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    Step 1: server I've created a simple server with Node & Socket.io which declares a namespace under ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I'm trying to use node to print the Serial from Arduino uno connected to USB I have a file script ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I have yet another "Can't set headers after they are sent." problem. I've created a post router ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I have been looking at using the ssh2 module for sftp shipping of logs. However the cloud service hosting ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    My websocket application accepting connections like this: wsServer.on('request', function(request) { console.log( ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    app.route('/users') .post(user.post) .get(user.get) .get(user.everyone) .put(user.update) . ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    Quick Summary of my question: Does Gulp 3.6.0 dest() handle glob-base the same way as 4.0.0? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    There are a lot of samples on running Async/Await functions. But only with one await call. I don' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 11, 2022 in Education by JackTerrance
...