我正在尝试将经过身份验证的数据提供者集成到 React-Admin 中,但是当数据提供者还没有来自身份验证提供者的有效身份验证令牌时,我遇到了麻烦。
我正在使用https://github.com/des-des/ra-postgraphile-client
我实例化数据提供者的代码如下所示:
const dataProvider = buildPostgraphileProvider({
apolloHttpLinkOptions: {
uri: `http://localhost:5433/graphql`,
fetch: (url, options) => {
// You can add custom auth logic with a wrapper around fetch
const token = localStorage.getItem("token");
if (!token) {
return;
}
if (jwt.decode(token).exp < Date.now()) {
localStorage.removeItem("token");
return;
}
const headers = {
...options.headers,
authorization: `bearer ${token}`
};
return fetch(url, { ...options, headers });
}
}
});
console.log(dataProvider);
我预计当 React-Admin 收到来自数据提供者的错误时,它会重定向回主页。但相反,它会引发未处理的拒绝。
Unhandled Rejection (Error): Network error: Cannot read property 'then' of undefined
new ApolloError
src/errors/ApolloError.ts:46
43 | // Constructs an instance of ApolloError given a GraphQLError
44 | // or a network error. Note that one of these has to be a valid
45 | // value or the constructed error will be meaningless.
> 46 | constructor({
| ^ 47 | graphQLErrors,
48 | networkError,
49 | errorMessage,
当没有令牌存在时,我还尝试返回 HTTPError:
return new HttpError("denied", 403, { status: 403 });
并得到了不同的结果:
Unhandled Rejection (Error): Network error: fetcher(...).then is not a function