Mongoose 完整的用户配置文件
我想为当前登录的用户做一个“完整的用户配置文件”,但是当我用邮递员测试api时,给出的结果显示为“unauthorized”。顺便说一句,我正在使用passport中间件。任何提示和帮助都将提前感谢:) 以下是我的更新代码:Mongoose 完整的用户配置文件,mongoose,jwt,passport.js,mern,Mongoose,Jwt,Passport.js,Mern,我想为当前登录的用户做一个“完整的用户配置文件”,但是当我用邮递员测试api时,给出的结果显示为“unauthorized”。顺便说一句,我正在使用passport中间件。任何提示和帮助都将提前感谢:) 以下是我的更新代码: router.post('/myprofile', passport.authenticate('jwt',{session: false}), (req,res)=>{ const {errors, isValid}= validateProfileInpu
router.post('/myprofile', passport.authenticate('jwt',{session: false}),
(req,res)=>{
const {errors, isValid}= validateProfileInput(req.body);
if(!isValid){
res.status(400).json(errors);
}
const profileFields={};
profileFields.user =req.user.id;
if(req.body.icNumber)profileFields.icNumber= req.body.icNumber;
if(req.body.phoneNumber)profileFields.phoneNumber= req.body.phoneNumber;
if(req.body.dob)profileFields.dob= req.body.dob;
if(req.body.gender)profileFields.gender= req.body.gender;
if(req.body.address1)profileFields.address1= req.body.address1;
if(req.body.address2)profileFields.address2= req.body.address2;
if(req.body.city) profileFields.city=req.body.city;
if(req.body.state) profileFields.state=req.body.state;
if(req.body.zip) profileFields.zip=req.body.zip;
Profile.findOne({user:req.user.id}).then(profile=>{
if(profile){
//update
Profile.findOneAndUpdate(
{user:req.user.id},
{$set: profileFields},
{$set: { status: true }}
).then(profile=>{
res.json(profile);
})
// save profile
new Profile(profileFields).save().then(profile=>res.json(profile))
}
})
}
)
邮递员抬头:
邮递员团体:
邮递员结果:
当您使用
jwt
创建令牌时,请使用一个基于您自己的数据创建令牌的函数…最后,令牌是令牌:“承载者”+签名令牌
,如下所示
const jsonwebtoken = require('jsonwebtoken');
function issueJWT(user) {
const _id = user._id;
const expiresIn = '1d';
const payload = {
sub: _id,
iat: Date.now()
};
const signedToken = jsonwebtoken.sign(payload, PRIV_KEY, { expiresIn: expiresIn, algorithm: 'RS256' });
return {
token: "Bearer " + signedToken,
expires: expiresIn
}
}
当你想得到请求时,就这样设置你的邮递员
或者在标题授权键中:Breare+spcace+令牌在您应该使用的标题旁边有一个
授权
选项卡。你试过了吗?passport.authenticate('jwt')…
意味着策略是jwt
,因此授权头应该是authentication:jwt eyJ….
我试过sinanspd和jps建议的ans,但它不能工作