NodeJS TypeError:JwtStrategy需要一个函数来从请求中检索jwt(参见选项jwtFromRequest)

mm9b1k5b  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(99)

我已经尝试了所有的东西,我已经遵循了StackOverflow中的所有链接,但问题没有解决。我已经改变了版本的护照-jwt。请务必照办。先谢谢你了。密码:我已经通过https://www.npmjs.com/package/passport-jwt#migrating-from-2xx-to-3xx所有的东西都是,但它也是给错误。

/home/hadi/Desktop/letuscode/node_modules/passport-jwt/lib/strategy.js:55
        throw new TypeError('JwtStrategy requires a function to retrieve jwt from requests (see option jwtFromRequest)');
        ^

TypeError: JwtStrategy requires a function to retrieve jwt from requests (see option jwtFromRequest)
    at new JwtStrategy (/home/hadi/Desktop/letuscode/node_modules/passport-jwt/lib/strategy.js:55:15)
    at module.exports (/home/hadi/Desktop/letuscode/config/passport.js:13:18)
    at Object.<anonymous> (/home/hadi/Desktop/letuscode/app.js:29:29)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47
passport.js

const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const mongoose = require('mongoose');
const User = mongoose.model('users');
const keys = require('../config/keys');

const opts = {};

opts.jwtFromrequest = ExtractJwt.fromAuthHeaderWithScheme('jwt')
opts.secretOrKey = keys.secretkey

module.exports = passport => {
    passport.use(new JwtStrategy(opts,function(jwt_payload,done){
            User.findById(jwt_payload.id)
                .then(user=>{
                    if(user){
                        return done(null,user)
                    }
                    return done(null,false)
                })
                .catch(err=>console.log(err))
        }))
}
app.js

const express = require('express');
const app = express();
const mongoose = require('mongoose');
const port =process.env.port;
const users = require('./routes/api/users');
const bodyparser = require('body-parser');
const passport = require('passport');

//DB config
const db = require('./config/keys').mongourl;

//connect to mongodb
mongoose
    .connect(db,{useNewUrlParser:true,useUnifiedTopology:true})
    .then(()=>console.log('mongodb conected'))
    .catch( err => console.group(err))

//bodyparser middleware
app.use(bodyparser.urlencoded({extended:false}))
app.use(bodyparser.json());

//use routes
app.use('/api/users',users);

//passport middleware
app.use(passport.initialize());

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

app.listen(port,()=>console.log(`server running on ${port}`));
routes.js

const express = require('express');
const router = express.Router();
const User = require('../../models/user');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const keys = require('../../config/keys');
const passport = require('passport');

//@route    GET /api/users/current
//@desc     return current user from token
//@access   private
router.get('/current',passport.authenticate('jwt',{session : false}),(req,res)=>{
    res.json({msg:'Success'});
})
xriantvc

xriantvc1#

你写代码时犯了个错误。请检查您的代码,并将其与下面的固定版本进行比较:
原代码:
jwtFromrequest: ExtractJwt.fromAuthHeaderWithScheme('jwt')
固定代码:
jwtFormRequest: ExtractJwt.fromAuthHeaderWithScheme("jwt")
也许这是一个问题!

相关问题