Skip to content

Commit afeaedd

Browse files
tnorlingCopilot
andauthored
New msal-browser sample enabling version switching at runtime (#7922)
Adds a new sample that includes the ability to switch versions at runtime (both local development builds and any published version). This will be used to test upgrade and downgrade scenarios both manually and in CI. --------- Co-authored-by: Copilot <[email protected]>
1 parent fbb3fbf commit afeaedd

File tree

24 files changed

+3613
-0
lines changed

24 files changed

+3613
-0
lines changed

package-lock.json

Lines changed: 126 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CLIENT_ID=ENTER_CLIENT_ID_HERE
2+
AUTHORITY=https://login.microsoftonline.com/ENTER_TENANT_ID_HERE
3+
REDIRECT_URI=http://localhost:3000
4+
POST_LOGOUT_REDIRECT_URI=http://localhost:3000
5+
CACHE_LOCATION=localStorage
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Environment variables
2+
.env
3+
.env.local
4+
.env.development
5+
.env.production
6+
7+
# Node modules
8+
node_modules/
9+
10+
# Logs
11+
logs
12+
*.log
13+
npm-debug.log*
14+
yarn-debug.log*
15+
yarn-error.log*
16+
17+
# Runtime data
18+
pids
19+
*.pid
20+
*.seed
21+
*.pid.lock
22+
23+
# Coverage directory used by tools like istanbul
24+
coverage/
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Dependency directories
30+
jspm_packages/
31+
32+
# Optional npm cache directory
33+
.npm
34+
35+
# Optional REPL history
36+
.node_repl_history
37+
38+
# Output of 'npm pack'
39+
*.tgz
40+
41+
# Yarn Integrity file
42+
.yarn-integrity
43+
44+
# dotenv environment variables file
45+
.env
46+
47+
# IDE and editor files
48+
.vscode/
49+
.idea/
50+
*.swp
51+
*.swo
52+
*~
53+
54+
# OS generated files
55+
.DS_Store
56+
.DS_Store?
57+
._*
58+
.Spotlight-V100
59+
.Trashes
60+
ehthumbs.db
61+
Thumbs.db
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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/)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "express-sample",
3+
"version": "1.0.0",
4+
"description": "Express sample app demonstrating MSAL.js authentication with routing",
5+
"main": "server.js",
6+
"scripts": {
7+
"start": "node server.js",
8+
"dev": "nodemon server.js",
9+
"build:package": "npm run build:all --workspace=lib/msal-browser"
10+
},
11+
"dependencies": {
12+
"@azure/msal-browser": "^4.0.0",
13+
"express": "^4.18.2",
14+
"express-handlebars": "^7.0.7",
15+
"morgan": "^1.10.0",
16+
"dotenv": "^16.0.3"
17+
},
18+
"devDependencies": {
19+
"nodemon": "^2.0.22"
20+
},
21+
"keywords": [
22+
"msal",
23+
"azure",
24+
"authentication",
25+
"express",
26+
"spa"
27+
],
28+
"author": "Microsoft",
29+
"license": "MIT"
30+
}

0 commit comments

Comments
 (0)