reactjs - 仅当未通过 @azure/msal-react 进行身份验证时才重定向 onLoad

标签 reactjs redirect azure-active-directory msal

我正在尝试调整 sample project为了我的需要。
我的需求基本上是:

  • 如果用户在导航到根路由时未通过身份验证,则自动重定向登录...
  • 如果是,则加载 protected 子组件。

  • 我有第 1 步。按预期工作。但是,在他们登录后,它似乎正在尝试再次重新路由,我得到:

    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

      70 | 
      71 | useEffect(() => {
      72 |   if (!isAuthenticated) {
    > 73 |     msalInstance.loginRedirect(loginRequest);
         | ^  74 |   }
      75 | })
      76 | 
    
    无论是否存在 !isAuthenticated,它都会这样做。有条件的。useIsAuthenticated的用法来自 this documentation并且似乎评估为 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',
    };
    
    
    // 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')
    );
    
    // 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;
    
    有关如何解决此问题的建议?

    最佳答案

    好的,我能够在一些帮助下解决这个问题:

    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;
    

    关于reactjs - 仅当未通过 @azure/msal-react 进行身份验证时才重定向 onLoad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66714416/

    相关文章:

    azure - 访客用户邀请未发送电子邮件

    reactjs - react : Ternary operation in JSX produces "Unexpected token" error

    asp.net - 如何关闭 Azure 网站中的 302 重定向

    PHP 重定向 - (SNI) 错误定向请求客户端需要一个新的连接 - 主机与服务器名称指示不匹配

    azure - 如何查询 Azure Active Directory 的应用程序客户端 key ?

    azure - 无法使用服务主体通过 Fluent .NET API 访问 Azure 资源管理器

    reactjs - 如何修复 “Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0” 错误

    javascript - react 键盘和输入之间的原生边距

    javascript - React Router,在参数之后嵌套路由

    php - URL 重定向到另一个域的子域