Axios headers 回调时缺少授权持有者
Axios headers authorization bearer is missing on callback
我目前在客户端应用程序上使用 Javascript
和 Axios
。
我 运行 一个 axios 请求(refreshToken();
)和另一个 axios
请求回调(userAuth();
),在第一个 axios
请求的响应中我收到一个新的令牌。
我尝试在回调的 headers authorization
承载中设置这个新令牌。
这不起作用:在回调 (userAuth();
) 中未设置新令牌,并且在 headers.
中不再设置授权承载
当 userAuth();
不是回调时,Authorization
Bearer
设置正确。
当userAuth();
为回调时,Authorization
Bearer
未设置。
let now = new Date();
let time = now.getTime();
time += 3600 * 1000;
now.setTime(time);
const setTokenOnCookie = (token) => {
document.cookie = 'token=' + token + '; expires=' + now.toUTCString();
}
const setRefreshOnCookie = (refresh_token) => {
document.cookie = 'refresh_token=' + refresh_token;
}
const TOKEN_USER = document.cookie.replace(/(?:(?:^|.*;\s*)token\s*\=\s*([^;]*).*$)|^.*$/, "");
const REFRESH_TOKEN = document.cookie.replace(/(?:(?:^|.*;\s*)refresh_token\s*\=\s*([^;]*).*$)|^.*$/, "");
const refreshToken = (userAuthCallback, userUnauthCallback) => {
axios.post(`${API_URL}/my/url/to/refresh/token`,
'refresh_token='+REFRESH_TOKEN,
{headers:{'Content-Type': 'application/x-www-form-urlencoded'}}
).then(res => {
Promise.all([setTokenOnCookie(res.data.token), setRefreshOnCookie(res.data.refresh_token)])
.then(()=>{
userAuthCallback(res.data.token);
}).catch(err => {
userUnauthCallback();
})
}
const userAuth = (token) => {
if(!token){
token = TOKEN_USER
}
axios.get(`${API_URL}/my/url/to/get/my/user`,
{headers:{'Authorization': `Bearer ${token}`}}
).then(res => {
pushToApplicationPath();
}).catch(err => {
catchMyError();
})
}
const userUnauth = () => {
document.cookie = 'token=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
document.cookie = 'refresh_token=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
document.cookie = 'username=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
pushToLoginPath();
}
refreshToken(userAuth, userUnauth);
你知道问题出在哪里吗?
我在 GitHub 上找到了未解决的问题。
我目前在客户端应用程序上使用 Javascript
和 Axios
。
我 运行 一个 axios 请求(refreshToken();
)和另一个 axios
请求回调(userAuth();
),在第一个 axios
请求的响应中我收到一个新的令牌。
我尝试在回调的 headers authorization
承载中设置这个新令牌。
这不起作用:在回调 (userAuth();
) 中未设置新令牌,并且在 headers.
当 userAuth();
不是回调时,Authorization
Bearer
设置正确。
当userAuth();
为回调时,Authorization
Bearer
未设置。
let now = new Date();
let time = now.getTime();
time += 3600 * 1000;
now.setTime(time);
const setTokenOnCookie = (token) => {
document.cookie = 'token=' + token + '; expires=' + now.toUTCString();
}
const setRefreshOnCookie = (refresh_token) => {
document.cookie = 'refresh_token=' + refresh_token;
}
const TOKEN_USER = document.cookie.replace(/(?:(?:^|.*;\s*)token\s*\=\s*([^;]*).*$)|^.*$/, "");
const REFRESH_TOKEN = document.cookie.replace(/(?:(?:^|.*;\s*)refresh_token\s*\=\s*([^;]*).*$)|^.*$/, "");
const refreshToken = (userAuthCallback, userUnauthCallback) => {
axios.post(`${API_URL}/my/url/to/refresh/token`,
'refresh_token='+REFRESH_TOKEN,
{headers:{'Content-Type': 'application/x-www-form-urlencoded'}}
).then(res => {
Promise.all([setTokenOnCookie(res.data.token), setRefreshOnCookie(res.data.refresh_token)])
.then(()=>{
userAuthCallback(res.data.token);
}).catch(err => {
userUnauthCallback();
})
}
const userAuth = (token) => {
if(!token){
token = TOKEN_USER
}
axios.get(`${API_URL}/my/url/to/get/my/user`,
{headers:{'Authorization': `Bearer ${token}`}}
).then(res => {
pushToApplicationPath();
}).catch(err => {
catchMyError();
})
}
const userUnauth = () => {
document.cookie = 'token=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
document.cookie = 'refresh_token=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
document.cookie = 'username=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
pushToLoginPath();
}
refreshToken(userAuth, userUnauth);
你知道问题出在哪里吗?
我在 GitHub 上找到了未解决的问题。