简体   繁体   English

如何使用刷新令牌刷新访问令牌?

[英]how to refresh access token with refresh token?

I have problem when access token expire and want to refresh it with token.我在访问令牌过期时遇到问题并想用令牌刷新它。 I have two methods, one which shows accounts and use axios post and another one which refresh token.我有两种方法,一种显示帐户并使用 axios post,另一种刷新令牌。

export const add_account = account=>{
    var token=localStorage.getItem("access_token")
      var decoded=jwt_decode(token);
        var time_exp=decoded.exp;
        if(time_exp<new Date().getTime()/1000) {
            refreshToken();
        }
    return axios.post("http://localhost:5000/accounts",{
        acc_name:account.name,
        acc_pass:account.pass,
        acc_host:account.host,
        acc_description:account.description
   }, {headers: {
    'Authorization': `Bearer ${localStorage.getItem('access_token')}`
        }}).then(res=>{
        return res.data
    })
}

Second method is refresh method and I notice in debug that first go to refresh token function and when entered in that function go to .then(res=> .... and after that go back to first function process return axios.post and see that access_token is expired.after that go back to refreshToken() and than set local storage access_token.第二种方法是刷新方法,我在调试中注意到首先转到刷新令牌函数,当进入该函数时转到.then(res=> .... 然后返回第一个函数进程返回 axios.post 并查看access_token 已过期。然后返回 refreshToken() 并设置本地存储 access_token。

export const refreshToken=()=>{
return axios.post("http://localhost:5000/refresh",null,{headers: {
'Authorization': `Bearer ${localStorage.getItem("refresh_token")}`
    }}).then(res=>{
        localStorage.setItem('access_token',res.data['access_token']);
        return res.data;
}).catch(e=>{
    console.log(e)
})

} }

It looks to me that the refreshToken function runs parallel to your login function because you do not wait till the refreshToken function is finished.在我看来,refreshToken 函数与您的登录函数并行运行,因为您不会等到 refreshToken 函数完成。 You could try to change the first one to an async function and await the result.您可以尝试将第一个更改为异步函数并等待结果。

export const add_account = async account=>{
    var token=localStorage.getItem("access_token")
      var decoded=jwt_decode(token);
        var time_exp=decoded.exp;
        if(time_exp<new Date().getTime()/1000) {
           await refreshToken();
        }

    return axios.post("http://localhost:5000/accounts",{
        acc_name:account.name,
        acc_pass:account.pass,
        acc_host:account.host,
        acc_description:account.description
   }, {headers: {
    'Authorization': `Bearer ${localStorage.getItem('access_token')}`
        }}).then(res=>{
        return res.data
    })
}

Maybe it would make more sense stylewise to also use the then syntax instead of async await, but then you would have to specify two paths of execution (one with refreshToken and one without), which seems convoluted.也许在风格上也使用 then 语法而不是 async await 会更有意义,但是你必须指定两条执行路径(一个有 refreshToken ,一个没有),这看起来很复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM