Skip to content

Commit 1ee80e0

Browse files
authored
Merge pull request #1641 from AzureAD/auth-sample-clean
clean up auth code sample a little
2 parents ba66362 + 82382c8 commit 1ee80e0

File tree

4 files changed

+32
-60
lines changed

4 files changed

+32
-60
lines changed

samples/msal-node-auth-code/index.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License.
44
*/
5-
6-
//initialize express
75
const express = require("express");
8-
const app = express();
9-
const port = 3000;
10-
app.get('/', (req, res) => redirectToAzureAd(req, res));
11-
app.get('/redirect', (req, res) => acquireToken(req, res));
6+
const msal = require('@azure/msal-node');
7+
8+
const SERVER_PORT = process.env.PORT || 3000;
129

1310
// initialize msal public client application
14-
let msal = require('@azure/msal-node');
1511
const publicClientConfig = {
1612
auth: {
1713
clientId: "99cab759-2aab-420b-91d8-5e3d8d4f063b",
@@ -24,27 +20,27 @@ const publicClientConfig = {
2420
storeAuthStateInCookie: false // Set this to "true" if you are having issues on IE11 or Edge
2521
}
2622
};
27-
2823
const pca = new msal.PublicClientApplication(publicClientConfig);
2924

30-
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
31-
32-
function redirectToAzureAd(req, res){
25+
// Create Express App and Routes
26+
const app = express();
3327

28+
app.get('/', (req, res) => {
3429
const authCodeUrlParameters = {
3530
scopes: ["user.read"],
3631
redirectUri: ["http://localhost:3000/redirect"],
3732
};
3833

34+
// get url to sign user in and consent to scopes needed for application
3935
pca.getAuthCodeUrl(authCodeUrlParameters)
4036
.then((response) => {
4137
console.log(response);
4238
res.redirect(response);
4339
})
4440
.catch((error) => console.log(JSON.stringify(error)));
45-
}
41+
});
4642

47-
function acquireToken(req, res){
43+
app.get('/redirect', (req, res) => {
4844
const tokenRequest = {
4945
code: req.query.code,
5046
redirectUri: "http://localhost:3000/redirect",
@@ -54,9 +50,11 @@ function acquireToken(req, res){
5450

5551
pca.acquireTokenByCode(tokenRequest).then((response) => {
5652
console.log(JSON.stringify(response));
57-
res.send(200);
53+
res.status(200).json(response);
5854
}).catch((error) => {
59-
res.send(500);
6055
console.log(JSON.stringify(error.response));
56+
res.status(500).send(JSON.stringify(error.response));
6157
})
62-
}
58+
});
59+
60+
app.listen(SERVER_PORT, () => console.log(`Msal Node Auth Code Sample app listening on port ${SERVER_PORT}!`))

samples/msal-node-auth-code/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "sample app for msal-node",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"start": "node index.js",
8+
"build:package": "cd ../../lib/msal-common && npm run build && cd ../msal-node && npm run build",
9+
"start:build": "npm run build:package && npm start"
810
},
911
"author": "sameerag",
1012
"license": "MIT",

samples/msal-node-device-code/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "Command line app that uses Oauth device code flow to get a token from Azure AD",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"start": "node index.js",
8+
"build:package": "cd ../../lib/msal-common && npm run build && cd ../msal-node && npm run build",
9+
"start:build": "npm run build:package && npm start"
810
},
911
"author": "",
1012
"license": "MIT",

samples/react-sample-app/package-lock.json

+12-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)