【发布时间】:2021-07-11 13:02:03
【问题描述】:
GET http://localhost:3000/user/authenticated 401 (Unauthorized) 对于某些不需要用户认证的请求,如何绕过这个错误。例如,电子商务商店能够向该网站的任何访问者展示所有产品,但卖家需要登录。那么我如何用我在下面创建的代码绕过这个呢?对不起,如果这解释得不好,我是后端和用户身份验证的新手。
请注意,我在获取数据方面没有问题,该过程运行良好。它只是出现错误,我想删除它。
以下是我如何设置用户身份验证错误源于 iaAuthenticated 部分,我将突出显示位置。
export default {
login : user => {
return fetch('/user/login', {
method : "post",
body : JSON.stringify(user),
headers: {
'Content-Type' : 'application/json'
}
}).then(res=>{
if(res.status !== 401)
return res.json().then(data => data);
else
return {isAuthenticated : false, user: {username : ""}}
})
},
register : user => {
return fetch('/user/register', {
method : "post",
body : JSON.stringify(user),
headers: {
'Content-Type' : 'application/json'
}
}).then(res => res.json())
.then(data => data);
},
logout : ()=> {
return fetch('/user/logout')
.then(res => res.json())
.then(data => data);
},
isAuthenticated : () => {
(error appears from this line)return fetch ('/user/authenticated')
.then(res=>{
if(res.status !== 401)
return res.json().then(data => data);
else
return {isAuthenticated : false, user: {username : ""}}
})
}
}
【问题讨论】:
-
能否显示后端代码(路由)?