Skip to content

Commit 6efcd89

Browse files
authored
Merge pull request #13 from Azure-Samples/fix-login-parameters
fix login request parameters, update readme
2 parents 11fb45d + 1c113bf commit 6efcd89

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

App/AuthProvider.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License.
44
*/
55

6-
const { PublicClientApplication } = require('@azure/msal-node');
6+
const { PublicClientApplication, InteractionRequiredAuthError } = require('@azure/msal-node');
77
const { shell } = require('electron');
88

99
class AuthProvider {
@@ -23,8 +23,13 @@ class AuthProvider {
2323
this.account = null;
2424
}
2525

26-
async login(tokenRequest) {
27-
const authResponse = await this.getToken(tokenRequest);
26+
async login() {
27+
const authResponse = await this.getToken({
28+
// If there are scopes that you would like users to consent up front, add them below
29+
// by default, MSAL will add the OIDC scopes to every token request, so we omit those here
30+
scopes: [],
31+
});
32+
2833
return this.handleResponse(authResponse);
2934
}
3035

@@ -40,9 +45,9 @@ class AuthProvider {
4045
if (this.account.idTokenClaims.hasOwnProperty('login_hint')) {
4146
await shell.openExternal(`${this.msalConfig.auth.authority}/oauth2/v2.0/logout?logout_hint=${encodeURIComponent(this.account.idTokenClaims.login_hint)}`);
4247
}
43-
48+
4449
await this.cache.removeAccount(this.account);
45-
this.account = null;
50+
this.account = null;
4651
} catch (error) {
4752
console.log(error);
4853
}
@@ -56,7 +61,6 @@ class AuthProvider {
5661
tokenRequest.account = account;
5762
authResponse = await this.getTokenSilent(tokenRequest);
5863
} else {
59-
console.log('get token interactive');
6064
authResponse = await this.getTokenInteractive(tokenRequest);
6165
}
6266

@@ -67,8 +71,12 @@ class AuthProvider {
6771
try {
6872
return await this.clientApplication.acquireTokenSilent(tokenRequest);
6973
} catch (error) {
70-
console.log('Silent token acquisition failed, acquiring token interactive');
71-
return await this.getTokenInteractive(tokenRequest);
74+
if (error instanceof InteractionRequiredAuthError) {
75+
console.log('Silent token acquisition failed, acquiring token interactive');
76+
return await this.getTokenInteractive(tokenRequest);
77+
}
78+
79+
console.log(error);
7280
}
7381
}
7482

@@ -82,7 +90,7 @@ class AuthProvider {
8290
...tokenRequest,
8391
openBrowser,
8492
successTemplate: '<h1>Successfully signed in!</h1> <p>You can close this window now.</p>',
85-
failureTemplate: '<h1>Oops! Something went wrong</h1> <p>Check the console for more information.</p>',
93+
errorTemplate: '<h1>Oops! Something went wrong</h1> <p>Check the console for more information.</p>',
8694
});
8795

8896
return authResponse;

App/main.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ const path = require("path");
77
const { app, ipcMain, BrowserWindow } = require("electron");
88

99
const AuthProvider = require("./AuthProvider");
10-
1110
const { IPC_MESSAGES } = require("./constants");
1211
const { protectedResources, msalConfig } = require("./authConfig");
1312
const getGraphClient = require("./graph");
1413

15-
const authProvider = new AuthProvider(msalConfig);
14+
let authProvider;
1615
let mainWindow;
1716

1817
function createWindow() {
@@ -22,11 +21,7 @@ function createWindow() {
2221
webPreferences: { preload: path.join(__dirname, "preload.js") },
2322
});
2423

25-
mainWindow.on('show', () => {
26-
setTimeout(() => {
27-
mainWindow.focus();
28-
}, 200);
29-
});
24+
authProvider = new AuthProvider(msalConfig);
3025
}
3126

3227
app.on("ready", () => {
@@ -52,7 +47,6 @@ ipcMain.on(IPC_MESSAGES.LOGIN, async () => {
5247
const account = await authProvider.login();
5348

5449
await mainWindow.loadFile(path.join(__dirname, "./index.html"));
55-
mainWindow.show();
5650

5751
mainWindow.webContents.send(IPC_MESSAGES.SHOW_WELCOME_MESSAGE, account);
5852
});
@@ -61,7 +55,6 @@ ipcMain.on(IPC_MESSAGES.LOGOUT, async () => {
6155
await authProvider.logout();
6256

6357
await mainWindow.loadFile(path.join(__dirname, "./index.html"));
64-
mainWindow.show();
6558
});
6659

6760
ipcMain.on(IPC_MESSAGES.GET_PROFILE, async () => {
@@ -73,9 +66,9 @@ ipcMain.on(IPC_MESSAGES.GET_PROFILE, async () => {
7366
const account = authProvider.account;
7467

7568
await mainWindow.loadFile(path.join(__dirname, "./index.html"));
76-
mainWindow.show();
7769

78-
const graphResponse = await getGraphClient(tokenResponse.accessToken).api(protectedResources.graphMe.endpoint).get();
70+
const graphResponse = await getGraphClient(tokenResponse.accessToken)
71+
.api(protectedResources.graphMe.endpoint).get();
7972

8073
mainWindow.webContents.send(IPC_MESSAGES.SHOW_WELCOME_MESSAGE, account);
8174
mainWindow.webContents.send(IPC_MESSAGES.SET_PROFILE, graphResponse);

README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,25 @@ urlFragment: "ms-identity-javascript-nodejs-desktop"
1515

1616
This sample demonstrates how to use [MSAL Node](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-node) to sign-in a user and acquire an access token for a protected resource such as Microsoft Graph in an Electron desktop application using the [authorization code grant with PKCE](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-auth-code-flow) flow.
1717

18+
This sample backs the following articles on Microsoft Docs:
19+
20+
- [Quickstart: Acquire a token and call Microsoft Graph API from a desktop application](https://learn.microsoft.com/azure/active-directory/develop/desktop-app-quickstart?pivots=devlang-nodejs-electron)
21+
- [Tutorial: Sign in users and call the Microsoft Graph API in an Electron desktop app](https://learn.microsoft.com/azure/active-directory/develop/tutorial-v2-nodejs-desktop)
22+
1823
> :information_source: Looking for a TypeScript implementation? See: [ElectronTestApp](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-node-samples/ElectronTestApp)
1924
2025
> :information_source: Looking for an Electron with React implementation? See: [ElectronReactTestApp](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-node-samples/ElectronReactTestApp)
2126
27+
> :warning: This sample does not implement persistent caching. See [Caching with MSAL Node](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/caching.md) for more information.
28+
2229
## Features
2330

2431
This sample demonstrates the following **MSAL Node** concepts:
2532

26-
* Configuration
27-
* Login and logout
28-
* Acquiring an access token
29-
* Calling a web API
33+
- Configuration
34+
- Login and logout
35+
- Acquiring an access token
36+
- Calling a web API
3037

3138
## Contents
3239

@@ -45,8 +52,8 @@ This sample demonstrates the following **MSAL Node** concepts:
4552

4653
### Prerequisites
4754

48-
* [Node.js](https://nodejs.org/en/) must be installed to run this sample.
49-
* [Visual Studio Code](https://code.visualstudio.com/download) is recommended for running and editing this sample.
55+
- [Node.js](https://nodejs.org/en/) must be installed to run this sample.
56+
- [Visual Studio Code](https://code.visualstudio.com/download) is recommended for running and editing this sample.
5057

5158
### Register and Setup the application
5259

@@ -68,7 +75,7 @@ This sample demonstrates the following **MSAL Node** concepts:
6875
1. Select **Add optional claim**:
6976
1. Select **optional claim type**, then choose **ID**.
7077
1. Select the optional claim **login_hint**.
71-
> An opaque, reliable login hint claim. This claim is the best value to use for the login_hint OAuth parameter in all flows to get SSO.See $[optional claims](https://docs.microsoft.com/azure/active-directory/develop/active-directory-optional-claims) for more details on this optional claim.
78+
> An opaque, reliable login hint claim. This claim is the best value to use for the login_hint OAuth parameter in all flows to get SSO. See $[optional claims](https://docs.microsoft.com/azure/active-directory/develop/active-directory-optional-claims) for more details.
7279
1. Select **Add** to save your changes.
7380

7481
#### Step 2: Clone the repository
@@ -80,12 +87,8 @@ Clone this repository `git clone https://github.com/Azure-Samples/ms-identity-ja
8087
1. Open the [.authConfig.js](./App/authConfig.js) file and provide the required configuration values.
8188
1. Replace the string `Enter_the_Application_Id_Here` with your app/client ID on Azure AD portal.
8289
1. Replace the string `Enter_the_Tenant_Info_Here` with your tenant ID on Azure AD portal.
83-
1. Replace the string `Enter_the_Cloud_Instance_Id_Here` with `https://login.microsoftonline.com/` (see **note** below).
84-
1. Replace the string `Enter_the_Graph_Endpoint_Here`. with `https://graph.microsoft.com/` (see **note** below).
85-
86-
> :information_source: *note*: This is for multi-tenant applications located on the Global Azure cloud. For more information, see: [Use MSAL in a national cloud environment](https://docs.microsoft.com/azure/active-directory/develop/authentication-national-cloud)
87-
88-
> :information_source: *note*: This is for MS Graph instance located on the Global Azure cloud. For more information, see: [Use Microsoft Graph in a national cloud environment](https://docs.microsoft.com/graph/deployments)
90+
1. Replace the string `Enter_the_Cloud_Instance_Id_Here` with `https://login.microsoftonline.com/` (include the trailing slash).
91+
1. Replace the string `Enter_the_Graph_Endpoint_Here`. with `https://graph.microsoft.com/` (include the trailing slash).
8992

9093
#### step 4: Run the sample
9194

0 commit comments

Comments
 (0)