# Initialization of MSAL Before you get started, please ensure you have completed all the [prerequisites](../README.md#prerequisites). In this document: - [Initialization of MSAL](#initialization-of-msal) - [CreatePCA pattern](#createpca-pattern) - [Standard Configuration](#standard-configuration) - [Nested App Configuration](#nested-app-configuration) - [Initializing the PublicClientApplication object](#initializing-the-publicclientapplication-object) - [(Optional) Configure Authority](#optional-configure-authority) - [(Optional) Configure Redirect URI](#optional-configure-redirect-uri) - [(Optional) Additional Configuration](#optional-additional-configuration) - [Choosing an Interaction Type](#choosing-an-interaction-type) - [Popup APIs](#popup-apis) - [Redirect APIs](#redirect-apis) - [Next Steps](#next-steps) ## CreatePCA pattern We are introducing a `CreatePCA` pattern in MSAL JS, to allow for apps to choose the type of `PublicClientApplication` they prefer. Current options include `Standard` and `Nestable` configurations. In the future, we will extend this to introduce more configurations. ### Standard Configuration If you are using MSAL.js in a single-page application, you should import msal-browser to create an [IPublicClientApplication](.\lib\msal-browser\src\app\IPublicClientApplication.ts) instance with `createStandardPublicClientApplication`. This function will create a PublicClientApplication instance with the standard configuration. ```javascript import * as msal from "@azure/msal-browser"; const pca = msal.createStandardPublicClientApplication({ auth: { clientId: "ENTER_CLIENT_ID", authority: "https://login.microsoftonline.com/ENTER_TENANT_ID", }, }); ``` ### Nested App Configuration If your app is an iframed Nested app and delegating its authentication to a hub SDK (which is either a SPA or a desktop application running in MetaOS framework), you should import msal-browser to create a IPublicClientApplication instance with `createNestablePublicClientApplication`. This function will create a PublicClientApplication instance with the NAA configuration. ```javascript import * as msal from "@azure/msal-browser"; const nestablePca = msal.createNestablePublicClientApplication({ auth: { clientId: "ENTER_CLIENT_ID", authority: "https://login.microsoftonline.com/ENTER_TENANT_ID", }, }); ``` Please note the below guidance before opting in for Nested app authentication: - `createNestablePublicClientApplication` will fall back to `createStandardPublicClientApplication` if nested app bridge is unavailable or the Hub is not configured to support nested app authentication. - If an application does not want to be Nested App, it should use `createStandardPublicClientApplication` instead. - Certain account lookup APIs are not supported in NAA apps, please refer to [active accounts](./accounts.md#active-account-apis). - **Cross-origin iframe scenarios:** If your app runs inside a cross-origin iframe, NAA is the **recommended** approach for authentication. - In browsers that enforce [third-party storage partitioning](https://developers.google.com/privacy-sandbox/cookies/storage-partitioning) (Chromium 115+, with other browsers expected to follow), popup flows (`loginPopup`, `acquireTokenPopup`) will fail because the popup opens as its own top-level context, placing it in a different `BroadcastChannel` partition than the iframe — the [redirect bridge's](./redirect-bridge.md) response never arrives. NAA avoids these limitations by delegating authentication to the host application at the top level. - For more details, see [Redirect Bridge — Cross-Origin Iframe Limitation](./redirect-bridge.md#cross-origin-iframe-limitation) and [Using MSAL in iframed apps](./iframe-usage.md). ## Initializing the PublicClientApplication object In order to use MSAL.js, you need to instantiate a `PublicClientApplication` object. You must provide the `client id` (`appId`) of your application. ### Option 1 Instantiate a `PublicClientApplication` object and initialize it afterwards. The `initialize` function is asynchronous and must resolve before invoking other MSAL.js APIs. ```javascript import { PublicClientApplication } from "@azure/msal-browser"; const msalConfig = { auth: { clientId: "your_client_id", }, }; const msalInstance = new PublicClientApplication(msalConfig); await msalInstance.initialize(); ``` ### Option 2 Invoke the `createPublicClientApplication` static method which returns an initialized `PublicClientApplication` object. Note that this function is asynchronous. ```javascript import { PublicClientApplication } from "@azure/msal-browser"; const msalConfig = { auth: { clientId: "your_client_id", }, }; const msalInstance = await PublicClientApplication.createPublicClientApplication(msalConfig); ``` ## (Optional) Configure Authority By default, MSAL is configured with the `common` tenant, which is used for multi-tenant applications and applications allowing personal accounts (not B2C). ```javascript const msalConfig = { auth: { clientId: "your_client_id", authority: "https://login.microsoftonline.com/common/", }, }; ``` If your application audience is a single tenant, you must provide an authority with your tenant id like below: ```javascript const msalConfig = { auth: { clientId: "your_client_id", authority: "https://login.microsoftonline.com/{your_tenant_id}", }, }; ``` If your application is using a separate OIDC-compliant authority like `"https://login.live.com"` or an IdentityServer, you will need to provide it in the `knownAuthorities` field and set your `protocolMode` to `"OIDC"`. ```javascript const msalConfig = { auth: { clientId: "your_client_id", authority: "https://login.live.com", knownAuthorities: ["login.live.com"], }, system: { protocolMode: "OIDC", } }; ``` **Note**: The `protocolMode` configuration option, which tells MSAL whether to enable AAD-specific quirks, changes the following behavior: - Authority metadata (since `v2.4.0`): - When set to `OIDC`, the library will not include `/v2.0/` in the authority path when fetching authority metadata. - When set to `AAD` (the default value), the library will include `/v2.0/` in the authority path when fetching authority metadata. For more information on authority, please refer to: [Authority in MSAL](../../msal-common/docs/authority.md). ## (Optional) Configure Redirect URI By default, MSAL is configured to set the redirect URI to the current page that it is running on. If you would like to receive the authorization code on a different page than the one running MSAL, you can set this in the configuration: ```javascript const msalConfig = { auth: { clientId: "your_client_id", authority: "https://login.microsoftonline.com/{your_tenant_id}", redirectUri: "https://contoso.com", }, }; ``` Any redirect URI used must be configured in the portal registration. You can also set the redirect URI per request using the [login](./login-user.md) and [request APIs](./acquire-token.md). ## (Optional) Additional Configuration MSAL has additional configuration options which you can review [here](./configuration.md). ## Handling App Launch with 0 or More Available Accounts The following flow diagram can help you avoid unnecessary authentication prompts when an account (or multiple accounts) is available for SSO. ![MSAL.js boot flow diagram](images/msaljs-boot-flow.png) ## Choosing an Interaction Type In the browser, there are two ways you can present the login screen to your users from your application: - [presenting a popup window from the current page](#popup-apis) - [redirecting the browser window to the login server](#redirect-apis) ### Popup APIs - `loginPopup` - `acquireTokenPopup` The popup APIs use ES6 Promises that resolve when the authentication flow in the popup concludes and returns to the redirect URI specified, or reject if there are issues in the code or the popup is blocked. > [!WARNING] > Popup flows can be unreliable in Safari and mobile browsers due to popup blocker and gesture constraints, especially when popup calls are made after async operations. For the most reliable cross-browser behavior, prefer redirect flows. For details and browser-specific guidance, see [Browser-specific guidance](./browser-specific-guidance.md). #### RedirectUri Considerations When using popup APIs, the `redirectUri` must point to a dedicated page that implements the MSAL redirect bridge. This page handles the authentication response and communicates it back to the main application. For detailed guidance on setting up the redirect page, see [RedirectUri considerations](./login-user.md#redirecturi-considerations). ```javascript msalInstance.loginPopup({ redirectUri: "http://localhost:3000/redirect", }); ``` ### Redirect APIs - `loginRedirect` - `acquireTokenRedirect` > [!TIP] > Redirect APIs are generally the most reliable interaction type across browsers, including Safari and mobile browsers. See [Browser-specific guidance](./browser-specific-guidance.md) for recommended patterns. Note: If you are using `msal-angular` or `msal-react`, redirects are handled differently, and you should see the [`msal-angular` redirect doc](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-angular/docs/redirects.md) and [`msal-react` FAQ](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-react/FAQ.md#how-do-i-handle-the-redirect-flow-in-a-react-app) for more details. The redirect APIs are asynchronous (i.e. return a promise) `void` functions which redirect the browser window after caching some basic info. If you choose to use the redirect APIs, be aware that **you MUST call `handleRedirectPromise()` to correctly handle the API**. You can use the following function to perform an action when this token exchange is completed: ```javascript msalInstance .handleRedirectPromise() .then((tokenResponse) => { // Check if the tokenResponse is null // If the tokenResponse !== null, then you are coming back from a successful authentication redirect. // If the tokenResponse === null, you are not coming back from an auth redirect. }) .catch((error) => { // handle error, either in the library or coming back from the server }); ``` It is not recommended to use both interaction types in a single application. ### Redirect response URL cleanup For redirect flows, `handleRedirectPromise()` removes recognized response fragments from the callback URL even when processing fails. **Note:** `handleRedirectPromise` will optionally accept a hash value to be processed, defaulting to the current value of `window.location.hash`. This parameter only needs to be provided in scenarios where the current value of `window.location.hash` does not contain the redirect response that needs to be processed. **For almost all scenarios, applications should not need to provide this parameter explicitly.** # Next Steps You are ready to perform a [login](./login-user.md)!