page_type | languages | name | description | products | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
|
Add Microsoft authentication to Express.js application |
JavaScript end-to-end add Microsoft authentication with MSAL to Express.js application |
|
For a complete tutorial, please use the Microsoft Documentation tutorial found here.
This sample illustrates how to integrate MSAL authentication for the Microsoft Identity provider into an Express.js app to provide:
- Login/logout
- Route restrictions
- Query a restricted API, such as Graph
-
Start by building the wrapper:
npm install
-
Then, configure required MSAL settings file in JSON (see: appSettings.json). The file looks like the following:
"credentials": { "clientId": process.env.AD_CLIENT_ID || "REPLACE-WITH-YOUR-APP-CLIENT-ID", "tenantId": process.env.AD_TENANT_ID || "REPLACE-WITH-YOUR-APP-TENANT-ID", "clientSecret": process.env.AD_CLIENT_ID_SECRET || "REPLACE-WITH-YOUR-APP-CLIENT-ID-SECRET" },
-
Run the project.
npm start
-
Open a browser at
http://localhost:8080
.
Session support in this sample is provided by the express-session package. express-session is considered unfit for production, and you should either implement your own session solution or use a more suitable 3rd party library.
MSAL Node has an in-memory cache by default. This sample also features a persistent cache plugin in order to save the cache to disk. This plugin is not meant to be production-ready. As such, you might want to implement persistent caching using a 3rd party library like redis.