Akryum / Akryum/vue-cli-plugin-apollo
Authlink runs before other links, preventing token refresh
- Ngôn ngữ chính
- JavaScript
- Star
- 477
- Fork
- 109
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
I'm using https://github.com/newsiberian/apollo-link-token-refresh to refresh my tokens. The process works fine, but the new access token won't be send in the request because authLink has already executed. It will work in the following requests.
### How it should work
1. GraphQL request starts
1. Refresh link detects access token has expired
1. Refresh link retrieves a new access token. Token is saved in localStorage
1. AuthLink sets the authorisation header from localStorage. New access token is used, the request will work
### How its working
1. GraphQL request starts
1. AuthLink sets the authorisation header from localStorage. Old expired access token is set
1. Refresh link detects access token has expired
1. Refresh link retrieves a new access token. Token is saved in localStorage
1. Request is sent with old token, so it fails.
1. Following requests will work, as now AuthLink will load the new token
### Possible solution
In [this issue](https://github.com/newsiberian/apollo-link-token-refresh/issues/2) from [apollo-link-token-refresh](https://github.com/newsiberian/apollo-link-token-refresh) is clearly stated that the link that performs the header setting (authLink) should be run last.
From the source code, I see that [authLink is being concatenated before all the other links](https://github.com/Akryum/vue-cli-plugin-apollo/blob/dfbad34582c2a5042e71ec2daacef69efd9be793/graphql-client/src/index.js#L87). Is it possible to concatenate it last? Will this break other functionality?
### Work Around
I've worked around this limitation by re-implementing the authLink and adding it after the refresh link.
```js
const accessTokenLink = setContext((_, { headers }) => {
// get your token
const token = localStorage.getItem(AUTH_TOKEN)
// add token to request headers
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : null
}
}
})
const link = ApolloLink.from([tokenRefreshLink, accessTokenLink ])
```
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Đánh giá
Issue này chưa được đánh giá.