问题描述
我正在尝试无缝登录用户,没有登录按钮,因为它将托管在 Azure 上。但以错误结束无限循环:
interaction_in_progress: Interaction is currently in progress.
Please ensure that this interaction has been completed before calling an interactive API.
For more visit: aka.ms/msaljs/browser-errors.
示例页面布局代码修改来自 https://github.com/AzureAD/microsoft-authentication-library-for-js 的 Typescript 示例:
export const PageLayout = (props: any) => {
const { instance,accounts,inProgress } = useMsal();
if(instance.getActiveAccount() == null)
{
console.log('account =' + instance.getActiveAccount());
instance.loginRedirect(loginRequest);
}
console.log('count =' + instance.getAllAccounts().length);
console.log('active =' + instance.getAllAccounts());
return (
<>
<Navbar bg="primary" variant="dark">
<a className="navbar-brand" href="/">MS Identity Platform</a>
{/* <SignInSignOutButton/> */}
</Navbar>
<h5 className="sample-header">Welcome to the Microsoft Authentication Library For Typescript - React Quickstart</h5>
<br/>
<br/>
{props.children}
</>
);
};
解决方法
这里解决了这个问题: https://github.com/AzureAD/microsoft-authentication-library-for-js/issues/3667
建议使用 useMsalAuthentication 钩子或 MsalAuthenticationTemplate component 实现页面加载时的自动登录。您可以使用此 sample 作为指南。