soc*_*pet 11 redirect reactjs azure-active-directory azure-ad-msal
我正在尝试根据我的需要调整示例项目。
我的需求本质上是:
我的步骤 1.按预期工作。然而,在他们登录后,它似乎正在尝试再次重新路由,我得到:
Interaction_in_progress:交互当前正在进行中。在调用交互API之前,请确保本次交互已经完成。有关更多信息,请访问:aka.ms/msaljs/browser-errors
70 |
71 | useEffect(() => {
72 | if (!isAuthenticated) {
> 73 | msalInstance.loginRedirect(loginRequest);
| ^ 74 | }
75 | })
76 |
Run Code Online (Sandbox Code Playgroud)
无论是否有条件,它都会执行此操作!isAuthenticated。
的用法useIsAuthenticated来自此文档false,即使用户已经登录,似乎也会进行评估。
这是我到目前为止所拥有的:
import { Configuration, RedirectRequest } from '@azure/msal-browser';
// Config object to be passed to Msal on creation
export const msalConfig: Configuration = {
auth: {
clientId: '<client_id>',
authority: 'https://login.microsoftonline.com/<tenant_id>',
},
};
// Add here scopes for id token to be used at MS Identity Platform endpoints.
export const loginRequest: RedirectRequest = {
scopes: ['User.Read'],
};
// Add here the endpoints for MS Graph API services you would like to use.
export const graphConfig = {
graphMeEndpoint: 'https://graph.microsoft.com/v1.0/me',
};
Run Code Online (Sandbox Code Playgroud)
// index.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
import { PublicClientApplication } from '@azure/msal-browser';
import { msalConfig } from './components/authConfig';
import { MsalProvider } from '@azure/msal-react';
const msalInstance = new PublicClientApplication(msalConfig);
ReactDOM.render(
<React.StrictMode>
<MsalProvider instance={msalInstance}>
<App />
</MsalProvider>
</React.StrictMode>,
document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)
// App.tsx
import React, { useEffect } from 'react';
import {
AuthenticatedTemplate,
UnauthenticatedTemplate,
useMsal,
useIsAuthenticated,
} from '@azure/msal-react';
import { loginRequest } from './authConfig';
const App = () => {
const isAuthenticated = useIsAuthenticated();
const { instance } = useMsal();
console.log(isAuthenticated);
useEffect(() => {
if (!isAuthenticated) {
instance.loginRedirect(loginRequest);
}
});
return (
<div className='App'>
<AuthenticatedTemplate>
<div>Signed in...</div>
</AuthenticatedTemplate>
<UnauthenticatedTemplate>
<div>Signing in...</div>
</UnauthenticatedTemplate>
</div>
);
};
export default App;
Run Code Online (Sandbox Code Playgroud)
关于如何解决这个问题的建议?
soc*_*pet 14
好的,我在一些帮助下解决了这个问题:
const App = () => {
const isAuthenticated = useIsAuthenticated();
const { instance, inProgress } = useMsal();
if (inProgress === InteractionStatus.None && !isAuthenticated) {
instance.loginRedirect(loginRequest);
}
return (
<div className='App'>
<AuthenticatedTemplate>
<div>Signed in...</div>
</AuthenticatedTemplate>
<UnauthenticatedTemplate>
<div>Signing in...</div>
</UnauthenticatedTemplate>
</div>
);
};
export default App;
Run Code Online (Sandbox Code Playgroud)
您可以简单地使用MsalAuthenticationTemplate 组件而不是AuthenticatedTemplate/ UnauthenticatedTemplate:
import React from "react";
import { MsalAuthenticationTemplate } from "@azure/msal-react";
import { InteractionType } from "@azure/msal-browser";
function ErrorComponent({error}) {
return <p>An Error Occurred: {error}</p>;
}
function LoadingComponent() {
return <p>Authentication in progress...</p>;
}
export function Example() {
const authRequest = {
scopes: ["openid", "profile"]
};
return (
// authenticationRequest, errorComponent and loadingComponent props are optional
<MsalAuthenticationTemplate
interactionType={InteractionType.Popup}
errorComponent={ErrorComponent}
loadingComponent={LoadingComponent}
>
<p>At least one account is signed in!</p>
</MsalAuthenticationTemplate>
)
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17917 次 |
| 最近记录: |