【发布时间】:2020-03-01 04:09:45
【问题描述】:
我已经按照他们的快速入门教程使用 React 设置了 Auth0。
基本上,我的 React 应用程序包裹在他们的 Context Provider 周围,我可以访问我的任何组件中的 useAuth0 钩子。
这就是我向我的 API 发出请求的方式:
const TestComponent = () => {
const { getTokenSilently } = useAuth0();
const getObjectsFromAPI = async () => {
const token = await getTokenSilently();
const axiosConfig = {
headers: {
Authorization: "Bearer " + token
}
};
const response = await axios.get(
"/api/objects/",
axiosConfig
);
// ... do something with the response
};
return ... removed code for brevity
};
有没有一种方法可以发出请求而不必在每个请求上写 token 和 axiosConfig?
我知道我可以使用配置初始化一个新的 axios 实例,但是我不能在上下文提供程序之外使用 useAuth0 钩子。
【问题讨论】: