问题:
注册用户时,一切正常。我也可以通过邮递员登录并获取“承载者令牌”。当我导航到名为“ / current”的路由并将承载令牌插入邮递员时,尽管登录并使用了当前承载令牌,但仍收到未授权的错误消息。
目标:
目标是当我登录时在控制台中接收有效负载详细信息(请参阅下面的passport.js文件)并在邮递员中接收成功消息。
文件和图像:
第一张图片中的注册成功
addAll(int index, Collection<? extends E> c)
下一张图片是我成功登录并收到承载令牌后的图片
我在下面的第一个图像中尝试了两种不同的方式,我在Postman中使用授权选项卡进行了尝试,第二种是标题,只是插入了我收到的授权和承载令牌。无论哪种方式,一旦我点击发送,我都会获得未经授权的授权,尽管当前令牌已安装到位。
以下是我的文件以users.js开头:
const express = require('express');
const router = express.Router();
const gravatar = require('gravatar');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const keys = require('../../config/keys');
const passport = require('passport');
//Load User Model
const User = require('../../models/User');
// @route GET api/users/test
// @desc Tests users route
// @access Public
router.get('/test', (req, res) => res.json({
msg: 'Users Works'
}));
// @route GET api/users/register
// @desc Register
// @access Public
// In order to use req.body you need to make sure you have body parser
router.post('/register', (req, res) => {
User.findOne({
email: req.body.email
}).then(user => {
if (user) {
return res.status(400).json({
email: 'Email Already Exists'
});
} else {
const avatar = gravatar.url(req.body.email, {
s: '200', //Size
r: 'pg', //Rating
d: 'mm' //Default
});
const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar,
password: req.body.password
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
newUser.password = hash;
newUser
.save()
.then(user => res.json(user))
.catch(err => console.log(err));
})
})
}
})
});
// @route GET api/users/login
// @desc Login user / Returning JWT Token
// @access Public
router.post('/login', (req, res) => {
const email = req.body.email;
const password = req.body.password;
//Find user by email
User.findOne({
email
})
.then(user => {
//Check for user
if (!user) {
return res.status(404).json({
email: 'User not found'
});
}
//check password
bcrypt.compare(password, user.password)
.then(isMatch => {
if (isMatch) {
//User Matched
const payload = {
id: user.id,
name: user.name,
avatar: user.avatar
} //Create JWT payload
//Sign Token
jwt.sign(payload,
keys.secretOrkey, {
expiresIn: 36000
},
(err, token) => {
res.json({
success: true,
token: 'Bearer ' + token
});
});
} else {
return res.status(400).json({
password: 'Password Incorrect'
});
}
});
});
});
// @route GET api/users/current
// @desc Return current user
// @access Private
router.get(
'/current',
passport.authenticate('jwt', {
session: false
}),
(req, res) => {
res.json({
msg: 'Success'
});
}
);
module.exports = router;
这是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.fromAuthHeaderAsBearerToken();
opts.secretOrKey = 'keys.secretOrKey';
module.exports = passport => {
passport.use(
new JwtStrategy(opts, (jwt_payload, done) => {
console.log(jwt_payload);
})
);
};