|
| 1 | +# MSAL.js for Express Sample - Authorization Code Flow in Single-Page Applications |
| 2 | + |
| 3 | +## About this sample |
| 4 | + |
| 5 | +This developer sample demonstrates how to use MSAL.js with an Express.js server to implement authentication and authorization in a single-page application. |
| 6 | + |
| 7 | +## Notable files and what they demonstrate |
| 8 | + |
| 9 | +### Core Application Files |
| 10 | +1. `./server.js` - Express server setup with routes and static file serving |
| 11 | +1. `./public/js/app.js` - Main application entry point |
| 12 | +1. `./public/js/authConfig.js` - Configuration options for `PublicClientApplication` and token requests |
| 13 | +1. `./public/js/auth.js` - **Authentication module** - MSAL instance management, login/logout flows, token handling |
| 14 | +1. `./public/js/ui.js` - **UI module** - UI updates, dropdown management, authentication state display |
| 15 | +1. `./public/js/account.js` - **Account module** - Account picker modal, account switching functionality |
| 16 | +1. `./public/js/navigation.js` - **Navigation module** - SPA routing, protected route handling, page transitions |
| 17 | +1. `./public/js/utils.js` - **Utility module** - Error handling, success messages, common utilities |
| 18 | +1. `./public/js/graph.js` - MS Graph API calls with access token handling |
| 19 | + |
| 20 | +## How to run the sample |
| 21 | + |
| 22 | +### Pre-requisites |
| 23 | + |
| 24 | +- Ensure [all pre-requisites](../../../lib/msal-browser/README.md#prerequisites) have been completed to run `@azure/msal-browser`. |
| 25 | +- Install node.js if needed (<https://nodejs.org/en/>). |
| 26 | + |
| 27 | +### Configure the application |
| 28 | + |
| 29 | +- Create a `.env` file in this directory and add the following variables: |
| 30 | + ``` |
| 31 | + CLIENT_ID=ENTER_CLIENT_ID_HERE |
| 32 | + AUTHORITY=https://login.microsoftonline.com/ENTER_TENANT_ID_HERE |
| 33 | + REDIRECT_URI=http://localhost:3000 |
| 34 | + POST_LOGOUT_REDIRECT_URI=http://localhost:3000 |
| 35 | + CACHE_LOCATION=localStorage |
| 36 | + ``` |
| 37 | + |
| 38 | +- Replace `ENTER_CLIENT_ID_HERE` with the Application (client) ID from the portal registration. |
| 39 | +- Replace `ENTER_TENANT_ID_HERE` with the tenant ID from the portal registration. |
| 40 | +- Optionally, you may replace any of the other parameters, or you can remove them and use the default values. |
| 41 | + |
| 42 | +#### Install npm dependencies for sample |
| 43 | + |
| 44 | +```bash |
| 45 | +# Install dev dependencies for msal-browser from root of repo |
| 46 | +npm install |
| 47 | + |
| 48 | +# Change directory to sample directory |
| 49 | +cd samples/msal-browser-samples/ExpressSample |
| 50 | + |
| 51 | +# Install sample dependencies |
| 52 | +npm install |
| 53 | + |
| 54 | +# Build packages locally |
| 55 | +npm run build:package |
| 56 | +``` |
| 57 | + |
| 58 | +#### Running the sample development server |
| 59 | + |
| 60 | +1. In a command prompt, run `npm start` or `npm run dev` (for auto-restart on changes). |
| 61 | +1. Open [http://localhost:3000](http://localhost:3000) to view it in the browser. |
| 62 | +1. Open [http://localhost:3000/profile](http://localhost:3000/profile) to see an example of a protected route. If you are not yet signed in, signin will be invoked automatically. |
| 63 | + |
| 64 | +#### Using the sample |
| 65 | + |
| 66 | +- In the web page, click on the "Sign In" button in the navigation to begin the auth flow. |
| 67 | +- You can choose between popup or redirect authentication methods. |
| 68 | +- Once authenticated, navigate to different pages to see how authentication state is preserved. |
| 69 | +- **Use the account switcher** by clicking the user account dropdown and selecting "Switch Account" to see the account picker modal. |
| 70 | +- The Profile page will automatically fetch and display your user information from MS Graph. |
| 71 | + |
| 72 | +## Learn more |
| 73 | + |
| 74 | +- [MSAL.js documentation](../../../lib/msal-browser/README.md) |
| 75 | +- [Express.js documentation](https://expressjs.com/) |
| 76 | +- [Microsoft Graph documentation](https://docs.microsoft.com/en-us/graph/) |
0 commit comments