Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 2.46 KB

README.md

File metadata and controls

70 lines (52 loc) · 2.46 KB
page_type languages name description products
sample
javascript
nodejs
Add Microsoft authentication to Express.js application
JavaScript end-to-end add Microsoft authentication with MSAL to Express.js application
azure
azure-portal
vs-code
msal-node
msal-js
microsoft-identity-web
identity
developer-tools
app-service

Add Microsoft authentication to Express.js application

For a complete tutorial, please use the Microsoft Documentation tutorial found here.

Express.js app

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

Usage

  1. Start by building the wrapper:

    npm install
  2. 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"
    },
  3. Run the project.

    npm start
  4. Open a browser at http://localhost:8080.

Remarks

Session support

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.

Persistent caching

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.

More information