Я хочу реализовать аутентификацию запроса, но эта ошибка все время:
Ошибка: аргумент передан
findByPk недействителен: [объект объекта]
в Function.findByPk (/home/admin/Desktop/project/node_modules/sequelize/lib/model.js:1714:13)
в Function.Model.findById (/home/admin/Desktop/project/node_modules/sequelize/lib/model.js:4209:25)
в JwtStrategy.passport.use.JwtStrategy [as _verify] (/home/admin/Desktop/project/middleware/passport.js:16:41)
в /home/admin/Desktop/project/node_modules/passport-jwt/lib/strategy.js:123:34
в /home/admin/Desktop/project/node_modules/jsonwebtoken/verify.js:196:12
в getSecret (/home/admin/Desktop/project/node_modules/jsonwebtoken/verify.js:76:14)
в Object.module.exports [как проверить] (/home/admin/Desktop/project/node_modules/jsonwebtoken/verify.js:80:10)
в Function.module.exports [as JwtVerifier] (/home/admin/Desktop/project/node_modules/passport-jwt/lib/verify_jwt.js:4:16)
в /home/admin/Desktop/project/node_modules/passport-jwt/lib/strategy.js:104:25
в JwtStrategy._secretOrKeyProvider (/home/admin/Desktop/project/node_modules/passport-jwt/lib/strategy.js:40:13)
по адресу JwtStrategy.authenticate (/home/admin/Desktop/project/node_modules/passport-jwt/lib/strategy.js:99:10)
при попытке (/home/admin/Desktop/project/node_modules/passport/lib/middleware/authenticate.js:361:16)
при аутентификации (/home/admin/Desktop/project/node_modules/passport/lib/middleware/authenticate.js:362:7)
в Layer.handle [as handle_request] (/home/admin/Desktop/project/node_modules/express/lib/router/layer.js:95:5)
в следующем (/home/admin/Desktop/project/node_modules/express/lib/router/route.js:137:13)
в Route.dispatch (/home/admin/Desktop/project/node_modules/express/lib/router/route.js:112:3)
в Layer.handle [as handle_request] (/home/admin/Desktop/project/node_modules/express/lib/router/layer.js:95:5)
в /home/admin/Desktop/project/node_modules/express/lib/router/index.js:281:22
в Function.process_params (/home/admin/Desktop/project/node_modules/express/lib/router/index.js:335:12)
в следующем (/home/admin/Desktop/project/node_modules/express/lib/router/index.js:275:10)
в Function.handle (/home/admin/Desktop/project/node_modules/express/lib/router/index.js:174:3)
на маршрутизаторе (/home/admin/Desktop/project/node_modules/express/lib/router/index.js:47:12)
Я использую PostgreSQL в качестве базы данных и Sequelize как ORM.
маршруты:
const express = require('express')
const router = express.Router()
const controller = require('../controllers/user')
const passport = require('passport')
// localhost:5000/api/admin/users
router.get('/users', passport.authenticate('jwt', {session: false}), controller.getAll)
module.exports = router
Паспорт:
const JwtStrategy = require('passport-jwt').Strategy
const ExtractJwt = require('passport-jwt').ExtractJwt
const db = require('../config/db.config.js')
const User = db.user
const options = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.SECRET_OR_KEY
}
module.exports = passport => {
passport.use(
new JwtStrategy(options, async (payload, done) => {
try {
const user = await User.findById({
where: {
id: payload.id
}
}).select('username id')
if (user) {
done(null, user)
} else {
done(null, false)
}
} catch(e) {
console.log(e)
}
})
)
}
Контроллер:
const db = require('../config/db.config.js')
const User = db.user
const errorHandler = require('../utils/errorHandler')
module.exports.getAll = async function(req, res) {
try {
const users = await User.findAll({user: req.user.id})
res.status(200).json(users)
} catch(e) {
errorHandler(req, e)
}
}