使用 Axios 进行 React-msal 的无效钩子调用问题
我正在我的应用程序中使用react-msal。我需要获取访问令牌并将其全局附加到 axios,但不幸的是,他们只提供挂钩来获取访问令牌(据我所知)。
到目前为止,这是我的 api.js 文件。
import axios from "axios";
import { useMsal } from "@azure/msal-react";
const axiosInstance = axios.create({
baseURL: "https://localhost:4211/api",
});
const { instance, accounts } = useMsal();
instance
.acquireTokenSilent({
...loginApiRequest,
account: accounts[0],
})
.then((response) => {
axiosInstance.defaults.headers.common[
"Authorization"
] = `Bearer ${response.accessToken}`;
})
.catch((error) => {
console("Error acquiring access token");
});
export default axiosInstance;
这是我在组件中调用 API 的地方。
api.get('/foods').then(response => {
alert(response.data)
}).catch(error => {
console.log(error.response)
})
但我遇到一个问题:错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。
这是显而易见的,但我需要替代方案来获取访问令牌并将其作为标头的一部分全局分配给我的 axios,这样我就不需要重写每次我需要调用端点时。
I'm using react-msal to my application. I need to acquire the access token and attach it to the axios globally, but unfortunately, they only provide hooks to get the access token (as far as I know).
So far, here's my api.js file.
import axios from "axios";
import { useMsal } from "@azure/msal-react";
const axiosInstance = axios.create({
baseURL: "https://localhost:4211/api",
});
const { instance, accounts } = useMsal();
instance
.acquireTokenSilent({
...loginApiRequest,
account: accounts[0],
})
.then((response) => {
axiosInstance.defaults.headers.common[
"Authorization"
] = `Bearer ${response.accessToken}`;
})
.catch((error) => {
console("Error acquiring access token");
});
export default axiosInstance;
And here's I call my API in my component.
api.get('/foods').then(response => {
alert(response.data)
}).catch(error => {
console.log(error.response)
})
But I'm getting an issue that says: Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
which is obvious but I need alternatives to get the access token and assign it to my axios globally as part of the header so I don't need to rewrite header each time I need to call an endpoints.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用传递到
MsalProvider
中的PublicClientApplication
实例。要获取帐户,请调用
instance.getAllAccounts()
。您无法在组件或上下文之外访问
inProgress
值,但由于您只是使用 acquireTokenSilent,因此您可能不需要它。下面是我的工作样本。
和下面的index.js
You can use
PublicClientApplication
instance passed into theMsalProvider
.To get the accounts call
instance.getAllAccounts()
.You can't access the
inProgress
value outside of a component or context, but since you're just using acquireTokenSilent you probably will not need it.below is my working sample.
and index.js below
对于未来的读者来说,拉维的解决方案是有效的。
仅替换
export const msalInstance = new PublicClientApplication(msalConfig());
与
export const msalInstance = new PublicClientApplication(msalConfig);
for future readers, Ravi's solution works.
Only replace
export const msalInstance = new PublicClientApplication(msalConfig());
with
export const msalInstance = new PublicClientApplication(msalConfig);
这是一个 React 应用程序,对吧?
您不能从 React 组件外部调用钩子或其他钩子。
https://reactjs.org/docs/hooks-rules.html
你可以这样做:
This is a React application, right?
You can't call hooks from outside of your React components, or other hooks.
https://reactjs.org/docs/hooks-rules.html
You could do something like this: