我在我的项目中使用 urql GraphQL 客户端。登录后,它不会自动在授权标头中添加令牌,因为它应该。我必须刷新一次页面,然后在标题中设置令牌。我已经阅读了文档,似乎无法弄清楚我做错了什么。
这是我的实现:
export const urqlClient = createClient({
url: `${url}`,
exchanges: [
dedupExchange,
cacheExchange,
authExchange({
getAuth: async ({ authState, mutate }: any) => {
// for initial launch, fetch the auth state from storage (local storage, async storage etc)
const token = Cookies.get('token');
if (!authState) {
if (token) {
return { token };
}
return null;
}
// refresh token
const decoded = jwt_decode<MyToken>(token as any);
if (typeof decoded.exp !== 'undefined' && decoded.exp < Date.now() / 1000) {
const user = firebase.auth().currentUser;
if (user) {
await user.getIdToken();
}
}
//auth has gone wrong. Clean up and redirect to a login page
Cookies.remove('token');
await signOut();
},
addAuthToOperation: ({ authState, operation }: any) => {
// the token isn't in the auth state, return the operation without changes
if (!authState || !authState.token) {
return operation;
}
// fetchOptions can be a function (See Client API) but you can simplify this based on usage
const fetchOptions =
typeof operation.context.fetchOptions === 'function'
? operation.context.fetchOptions()
: operation.context.fetchOptions || {};
return makeOperation(operation.kind, operation, {
...operation.context,
fetchOptions: {
...fetchOptions,
headers: {
...fetchOptions.headers,
Authorization: `Bearer ${authState.token}`,
},
},
});
},
}),
fetchExchange,
]
});
我在这里做错了什么?谢谢