-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2900 from juliemturner/version-4
Merge PR #2832
- Loading branch information
Showing
8 changed files
with
175 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,6 @@ | ||
# Authentication in a custom browser based application | ||
|
||
We support MSAL for both browser and nodejs by providing a thin wrapper around the official libraries. We won't document the fully possible MSAL configuration, but any parameters supplied are passed through to the underlying implementation. To use the browser MSAL package you'll need to install the @pnp/msaljsclient package which is deployed as a standalone due to the large MSAL dependency. | ||
This library provides a thin wrapper around the [@azure/msal-browser](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-browser/docs) library to make it easy to integrate MSAL authentication in the browser. | ||
|
||
`npm install @pnp/msaljsclient --save` | ||
|
||
At this time we're using version 1.x of the `msal` library which uses Implicit Flow. For more informaiton on the msal library please see the [AzureAD/microsoft-authentication-library-for-js](https://github.com/AzureAD/microsoft-authentication-library-for-js#readme). | ||
|
||
Each of the following samples reference a MSAL configuration that utilizes an Azure AD App Registration, these are samples that show the typings for those objects: | ||
|
||
```TypeScript | ||
import { Configuration, AuthenticationParameters } from "msal"; | ||
|
||
const configuration: Configuration = { | ||
auth: { | ||
authority: "https://login.microsoftonline.com/{tenant Id}/", | ||
clientId: "{AAD Application Id/Client Id}" | ||
} | ||
}; | ||
|
||
const authParams: AuthenticationParameters = { | ||
scopes: ["https://graph.microsoft.com/.default"] | ||
}; | ||
``` | ||
|
||
## MSAL + Browser | ||
|
||
```TypeScript | ||
import { spfi, SPBrowser } from "@pnp/sp"; | ||
import { graphfi, GraphBrowser } from "@pnp/graph"; | ||
import { MSAL } from "@pnp/msaljsclient"; | ||
import "@pnp/sp/webs"; | ||
import "@pnp/graph/users"; | ||
|
||
const sp = spfi("https://tenant.sharepoint.com/sites/dev").using(SPBrowser(), MSAL(configuration, authParams)); | ||
|
||
// within a webpart, application customizer, or adaptive card extension where the context object is available | ||
const graph = graphfi().using(GraphBrowser(), MSAL(configuration, authParams)); | ||
|
||
const webData = await sp.web(); | ||
const meData = await graph.me(); | ||
``` | ||
Please see more scenarios in the [authentication article](../msaljsclient/index.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,48 @@ | ||
# @pnp/msaljsclient | ||
|
||
This library provides a thin wrapper around the [msal](https://github.com/AzureAD/microsoft-authentication-library-for-js) library to make it easy to integrate MSAL authentication in the browser. | ||
This library provides a thin wrapper around the [@azure/msal-browser](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-browser/docs) library to make it easy to integrate MSAL authentication in the browser. | ||
|
||
You will first need to install the package: | ||
|
||
`npm install @pnp/msaljsclient --save` | ||
|
||
The configuration and authParams | ||
You may also need to install the MSAL library in typescript for future development with full type support: | ||
|
||
`npm install @azure/msal-browser --save-dev` | ||
|
||
The configuration | ||
|
||
```TypeScript | ||
import type { MSALOptions } from "@pnp/msaljsclient"; | ||
import { spfi, SPBrowser } from "@pnp/sp"; | ||
import { MSAL } from "@pnp/msaljsclient"; | ||
import { MSAL, getMSAL } from "@pnp/msaljsclient"; | ||
import "@pnp/sp/webs"; | ||
|
||
const configuation = { | ||
auth: { | ||
authority: "https://login.microsoftonline.com/common", | ||
clientId: "{client id}", | ||
import "@pnp/sp/site-users/web"; | ||
|
||
const options: MSALOptions = { | ||
configuration: { | ||
auth: { | ||
authority: "https://login.microsoftonline.com/{tanent_id}/", | ||
clientId: "{client id}", | ||
}, | ||
cache: { | ||
cacheLocation: "localStorage" // in order to avoid re-login after page refresh | ||
} | ||
}, | ||
authParams: { | ||
forceRefresh: false, | ||
scopes: ["https://{tenant}.sharepoint.com/.default"], | ||
} | ||
}; | ||
|
||
const authParams = { | ||
scopes: ["https://{tenant}.sharepoint.com/.default"], | ||
}; | ||
const sp = spfi("https://tenant.sharepoint.com/sites/dev").using(SPBrowser(), MSAL(options)); | ||
|
||
const sp = spfi("https://tenant.sharepoint.com/sites/dev").using(SPBrowser(), MSAL(configuration, authParams)); | ||
const user = await sp.web.currentUser(); | ||
|
||
const webData = await sp.web(); | ||
// For logout later on | ||
const msalInstance = getMSAL(); | ||
const currentAccount = msalInstance.getActiveAccount(); | ||
msalInstance.logoutPopup({ account: currentAccount }); | ||
``` | ||
|
||
Please see more scenarios in the [authentication article](../concepts/authentication.md). | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.