有没有办法在访问 SPA 时立即进行登录重定向?

Is there a way to do a login redirect immediately when the SPA is accessed?

我创建了一个 SPA。参考the docs.

因此,访问 SPA 并按 [登录] 按钮将带您进入登录屏幕。如果身份验证成功,您将被重定向到 SPA again.This 是一个常见的过程。 但是,我希望在访问 SPA 后立即看到登录页面。

换句话说,我们希望从一开始就显示登录页面,而不是按下 [SignIN] 按钮并且不触发 onclick 事件。 示例中,点击按钮后执行如下流程

function signIn() {

    myMSALObj.loginRedirect(loginRequest);
}

是否可以在 URL 打开时执行此功能并向用户显示登录屏幕? 我发现有一个叫做“onload”的东西,并尝试使用它,但是没有用。

谢谢。

您似乎希望您的整个应用程序受 Azure AD 身份验证保护。如果是这样,那么您可以做的就是将整个应用程序包装在 <MsalAuthenticationTemplate>.

此组件将确保用户必须 signed-in 才能访问应用程序。

这是从 link:

中截取的代码示例
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} 
            authenticationRequest={authRequest} 
            errorComponent={ErrorComponent} 
            loadingComponent={LoadingComponent}
        >
            <p>At least one account is signed in!</p>
        </MsalAuthenticationTemplate>
      )
};