• You can try the sign-in with redirect method that uses the MSAL React Wrapper element that allows you to protect specific components by wrapping them in the ‘MsalAuthenticationTemplate’ component. This component will invoke login if a user is not already signed in or render child components otherwise.
‘ import { InteractionType } from "@azure/msal-browser";
import { MsalAuthenticationTemplate, useMsal } from "@azure/msal-react";
function WelcomeUser() {
const { accounts } = useMsal();
const username = accounts[0].username;
return <p>Welcome, {username}</p>
}
// Remember that MsalProvider must be rendered somewhere higher up in the component tree
function App() {
return (
<MsalAuthenticationTemplate interactionType={InteractionType.Redirect}>
<p>This will only render if a user is signed-in.</p>
<WelcomeUser />
</MsalAuthenticationTemplate>
)
}; ’
Also, once the above has been done to allow Azure AD B2C MSAL authentication, ensure that implicit grant flow in your Azure AD B2C app registration has been allowed and a custom public client protocol as the redirect URI has been added like ‘company://auth’ or ‘msal://redirect’. While the ‘ADAL config’ for local development should be as below: -
‘ adalConfig: {
tenant: 'XXXXXXXXXXX',
clientId: 'XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
redirectUri: 'company://auth'
} ’
• The recommended approach to invoke login because of user interaction such as a button click is to use the ‘@azure/msal-browser’ directly paired with the AuthenticatedTemplate and/or UnauthenticatedTemplate components to render specific contents to signed-in or signed-out users respectively as shown in below example: -
‘ import { useMsal, AuthenticatedTemplate, UnauthenticatedTemplate } from "@azure/msal-react";
function signInClickHandler(instance) {
instance.loginRedirect();
}
// SignInButton Component returns a button that invokes a popup login when clicked
function SignInButton() {
// useMsal hook will return the PublicClientApplication instance you provided to MsalProvider
const { instance } = useMsal();
return <button onClick={() => signInClickHandler(instance)}>Sign In</button>
};
function WelcomeUser() {
const { accounts } = useMsal();
const username = accounts[0].username;
return <p>Welcome, {username}</p>
}
// Remember that MsalProvider must be rendered somewhere higher up in the component tree
function App() {
return (
<>
<AuthenticatedTemplate>
<p>This will only render if a user is signed-in.</p>
<WelcomeUser />
</AuthenticatedTemplate>
<UnauthenticatedTemplate>
<p>This will only render if a user is not signed-in.</p>
<SignInButton />
</UnauthenticatedTemplate>
</>
)
} ’
• Thus, by following the above specified modifications, you should be able to integrate Azure AD B2C with Electron react app correctly since, electron app uses javascript as its base. Please refer the below links for more information: -
https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-spa-sign-in?tabs=react#sign-in-with-redirect
https://github.com/AzureAD/microsoft-authentication-library-for-js/issues/324