我试图用不记名令牌从我的前面到我的 api 进行授权,在我的 api 中它的所有工作我都用 insmonia 进行测试,当我尝试以这种方式在标题中添加授权时,问题就在前面,我正在使用react-redux 用于存储,redux-persist 用于持久化数据,当我尝试添加时
export function setToken({ payload }) {
if (!payload) return;
const { token } = payload.auth;
if (token) {
api.defaults.headers.authorization = `Bearer ${token}`;
}
}
export default all([
takeLatest('@auth/persist/REHYDRATE', setToken),
takeLatest('@auth/SIGN_IN_REQUEST', signIn),
takeLatest('@auth/SIGN_UP_REQUEST', signUp),
]);
但是当我重新加载我的管理页面时,我在浏览器中进行 httprequests(post/get etc),它没有在标题中接收授权并说 createError.js:16 Uncaught (in promise) Error: Request failed with status code 401
Request URL: http://localhost:3333/notifications
Request Method: GET
Status Code: 401 Unauthorized
Remote Address: [::1]:3333
Referrer Policy: strict-origin-when-cross-origin
RESPONSE HEADERS
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 23
Content-Type: application/json; charset=utf-8
Date: Mon, 11 Jan 2021 00:52:14 GMT
ETag: W/"17-IQ4NhR9c983ggGeU6wL1lrE99jM"
Keep-Alive: timeout=5
X-Powered-By: Express
REQUEST HEADERS
Accept: application/json, text/plain, */
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Host: localhost:3333
Origin: http://localhost:3000
Pragma: no-cache
Referer: http://localhost:3000/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
Sec-GPC: 1
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Mobile Safari/537.36
我做错了什么?如何添加不记名和令牌的授权?
如果有助于理解问题,这是我的项目存储库 https://github.com/dariocoroneldev/Error