请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

401

确定后端能获取token,也已经登录了,但前端console报401,message是Not Found,视频重复看几遍了,完全是跟这些,麻烦老师帮看看吧,以下是代码:

const User = require(’…/models/users’);
const jwt = require(‘jsonwebtoken’);
const { secret } = require(’…/config’);

class UserCtrl {
async find(ctx) {
ctx.body = await User.find();
}
async findById(ctx) {
const { fields } = ctx.query;
const user = await User.findById(ctx.params.id);

    if(!user) {
        ctx.throw(404, '用户不存在');
    }
    ctx.body = user;
}
async create(ctx) {
    ctx.verifyParams({
        username: { type: 'string', required: true },
        password: { type: 'string', required: true }
    });
    const { username } = ctx.request.body;
    const repeatedUser = await User.findOne({ username });
    if(repeatedUser) {
        ctx.throw(409, '用户已经存在')
    }
    const user = await new User(ctx.request.body).save();
    ctx.body = user;
}
async checkOwner(ctx, next) {
    console.log('check')
    if(ctx.params.id !== ctx.state.user._id) {
        ctx.throw(403, '沒有權限');
    }
    await next();
}
async update(ctx) {
    console.log('update')
    ctx.verifyParams({
        username: { type: 'string', required: false },
        password: { type: 'string', required: false },
        avatar_url: { type: 'string', required: false },
        gender: { type: 'string', required: false },
        headline: { type: 'string', required: false },
        locations: { type: [{ type: String }] },
        business: { type: 'array', itemType: 'object', required: false },
        educations: { type: 'array', itemType: 'object', required: false },
    })
    const user = await User.findByIdAndUpdate(ctx.params.id, ctx.request.body)
    if(!user) {
        ctx.throw(404, '无此用户')
    }
    ctx.body = user
}
async delete(ctx) {
    ctx.status = 204
}
async login(ctx) {
    ctx.verifyParams({
        username: { type: 'string', required: true },
        password: { type: 'string', required: true }
    })
    
    const user = await User.findOne(ctx.request.body);
    if(!user) {
        ctx.throw(401, '用戶名或密碼不正確');
    }
    const { _id, username } = user;
    const token = jwt.sign({ _id, username}, secret, {expiresIn: '1d'});
    ctx.body = { token };
}

}

module.exports = new UserCtrl();

===================================

const Router = require(‘koa-router’);
const router = new Router({prefix: ‘/users’});
const { find, findById, create, delete:del, update, login, checkOwner } = require(’…/controllers/user’);
const jwt = require(‘koa-jwt’);
const { secret } = require(’…/config’);

const auth = jwt({
secret
});

router.get(’/’, find);
router.post(’/’, create);
router.get(’/:id’, findById);
router.patch(’/:id’, auth, checkOwner, update);
router.delete(’/:id’, auth, checkOwner, del);
router.post(’/login’, login);

module.exports = router;

正在回答 回答被采纳积分+3

3回答

提问者 慕后端1189009 2021-05-06 15:41:07

应该怎么设置啊,postman现在版本没有视频里选项

https://img1.sycdn.imooc.com//szimg/60939d8f0976fd9c07070412.jpg

0 回复 有任何疑惑可以回复我~
提问者 慕后端1189009 2021-05-06 15:36:45

首先是登录:

https://img1.sycdn.imooc.com//szimg/60939b7c092a91e509460360.jpg


https://img1.sycdn.imooc.com//szimg/60939bb20933d6d109680608.jpg




接着是修改:

https://img1.sycdn.imooc.com//szimg/60939c51095f415709800453.jpg

https://img1.sycdn.imooc.com//szimg/60939c7309689c9009680655.jpg


postman版本不一样,所以没跟着用bear




0 回复 有任何疑惑可以回复我~
lewis 2021-05-06 15:23:10

前端报401,是认证失败哦,你把postman 的header截图发我看下

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信