MSAL for M365 authentication: Failed to construct 'URL': Invalid URL

Miguel Lopes Isidoro 46 Reputation points
2024-02-26T12:13:39.3533333+00:00

Hello, when using the following code

const msalConfig = {
    auth: {
        clientId: '@Model.M365ClientId',
        authority: '@Model.Authority'
    },
    cache: {
        cacheLocation: "localStorage",
        storeAuthStateInCookie: true,
        secureCookies: true
    }
};

const msalInstance = new msal.PublicClientApplication(msalConfig);

var loginRequest = {
    scopes: ["user.read", "offline_access"],
    prompt: 'select_account',
    redirectUri: window.location.origin + '/blank.html'
};

try {
    await msalInstance.initialize();
    await msalInstance.handleRedirectPromise();

    const loginResponse = await msalInstance.loginPopup(loginRequest);

to open up a M365 Azure AD login popup I am getting the following error in the last line of the code above. Javascript code is being executed from a .js file.

login.js:80 TypeError: Failed to construct 'URL': Invalid URL
    at Qi (msal-browser-3.9.0.min.js:68:17534)
    at ga.acquireTokenPopupAsync (msal-browser-3.9.0.min.js:68:92392)
    at async HTMLButtonElement.login365 (login.js:55:31)

If I move the code from the .js file to an inline script, the auth popup is correctly displayed and I can login into my application. How can I solve this? I don't want to use an inline script. Thanks, Miguel

Microsoft 365 and Office | Install, redeem, activate | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Miguel Lopes Isidoro 46 Reputation points
    2024-03-27T15:09:30.71+00:00

    Hello,

    Can anyone reply to this question?

    Thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.