在PUT请求时获取未经授权的错误

时间:2018-02-13 07:57:25

标签: javascript angular http

正如标题所说,虽然请求中有令牌,但仍会收到未经授权的错误。

enter image description here

还有什么可能导致此错误?

这是请求:

editPost(id: string, Post: Post) {
    return this.http.put(environment.apiUrl + '/posts/' + id, Post);
  }

这是Angular应用程序,使用ngrx,如果您需要更多代码,我会编辑帖子..

编辑:

router.put('/posts/:id', passport.authenticate('jwt'), (req, res) => {

    Post.findOne({ _id: req.params.id }, (err, post) => {
        if (err) throw err;
        if (post.author.equals(req.user._id)) {
            post.title = req.body.title,
                post.content = req.body.content,
                post.postImageUrl = req.body.postImageUrl

            post.save((err, updatedPost) => {
                if (err) throw err;
                else {
                    res.json({ message: 'You have successfully updated your post', success: true });
                }
            });
        } else {

            res.json({ success: false, message: 'You are not allowed to do this.' });
        }
    });
});

这是auth拦截器,我认为问题可能在这里:

return this.store.select('auth')
            .take(1)
            .switchMap((authState: fromAuth.AuthState) => {
                const copiedReq = request.clone({ headers: request.headers.set('Authentication', 'Bearer ' + authState.token) });
                return next.handle(copiedReq);
            })

0 个答案:

没有答案