У меня нет времени, чтобы воспроизвести это. Ваша учетная запись выглядит правильно. Тем не менее, вы должны попытаться настроить защищенные маршруты, подобные этому, скопированные и адаптированные к вашим потребностям из первой статьи:
Настройка промежуточного программного обеспечения для обработки дешифрования jwt, обязательно укажите его в вашем app.js
или где бы вы ни находились нужно, если вы настроили его в отдельный файл. Это может быть использовано в качестве промежуточного программного обеспечения позже в ваших контроллерах:
const JWTstrategy = require('passport-jwt').Strategy;
//We use this to extract the JWT sent by the user
const ExtractJWT = require('passport-jwt').ExtractJwt;
//This verifies that the token sent by the user is valid
passport.use(new JWTstrategy({
//secret we used to sign our JWT
secretOrKey : PUB_KEY,
algorithms: ['HS256'],
//we expect the user to send the token as a query parameter with the name 'token'
jwtFromRequest : ExtractJWT.fromUrlQueryParameter('token')
}, async (token, done) => {
try {
//Pass the user details to the next middleware
return done(null, token.user);
} catch (error) {
done(error);
}
}));
Настройка защищенного маршрута, обратите внимание, что вам не нужно вручную вызывать jwt.verify
, промежуточное программное обеспечение обрабатывает его и заполняет req.user
:
const express = require('express');
const router = express.Router();
//Let's say the route below is very sensitive and we want only authorized users to have access
//Displays information tailored according to the logged in user
router.get('/profile', passport.authenticate('jwt', { session: false }), (req, res, next) => {
//We'll just send back the user details and the token
res.json({
message : 'You made it to the secure route',
user : req.user,
token : req.query.token
})
});
module.exports = router;
** Обновление на основе вашего комментария:
Я клонировал ваш репозиторий, и он работает для меня, хотя я кое-что изменил:
Я добавил app.use(bodyParser.json());
к app.js
, чтобы можно было отправлять тела запросов как json - в этом нет необходимости, если вы предпочитаете urlencoded
, проблема в том, что secureRoute
вы экспортируете другой маршрутизатор, и вы попытаетесь использовать его в качестве контроллера в app.js
:
...
const secureRoute = require('./routes/secure-routes');
...
app.use('/user', passport.authenticate('jwt', { session: false }), secureRoute);`
* обратите внимание, что это будет /user
маршрут, если вы хотите /profile
измените его, как app.use('/profile', ...)
, поэтому вместо
router.get("/profile", (req, res, next) => {
//We'll just send back the user details and the token
res.json({
message: "You made it to the secure route",
user: req.user,
token: req.query.secret_token
});
});
это должна быть просто функция контроллера:
...
module.exports = (req, res, next) => {
//We'll just send back the user details and the token
res.json({
message: 'You made it to the secure route',
user: req.user,
token: req.query.token // note that you don't use secret_token but token as a name
});
};
- третий нужно не забыть добавить токен в параметры запроса, когда вы вызываете API, например,
http://localhost:3000/user?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Il9pZCI6IjVlODc2Yjc1YTVlNTk3MTRlOGFjMmI4NyIsImVtYWlsIjoiYUBiLmNvbSJ9LCJpYXQiOjE1ODU5MzMyNjR9.lcLuQeCMRy7Ef9zNkIt_rn4S22t2cm7YLRE7Jgp1Mpw
, вы должны получить ответ se:
{
"message": "You made it to the secure route",
"user": {
"_id": "5e876b75a5e59714e8ac2b87",
"email": "a@b.com"
}
}