You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> This is the platform-specific Accounts documentation for `msal-node`. For the general documentation of the `AccountInfo` object structure, please visit the `msal-common`[Accounts document](../../msal-common/docs/Accounts.md).
19
-
20
18
## Usage
21
19
22
20
The `msal-node` library provides the following different APIs to access cached accounts:
Copy file name to clipboardExpand all lines: msal-javascript-conceptual/react/class-components.md
+35-10Lines changed: 35 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: Using MSAL React with class components
3
-
description: Learn how to use MSAL React with class components. covering initialization, protecting components, accessing MSAL React context and logging in.
3
+
description: Learn how to use MSAL React with class components, covering initialization, protecting components, accessing MSAL React context and logging in.
MSAL React supports both function components and class components. However, you will not be able to use `@azure/msal-react` hooks inside your class components so if you need access to authentication state inside your class component you will need to use `@azure/msal-browser` directly to obtain similar functionality.
15
+
MSAL React supports both function components and class components. This article provides guidance on using MSAL React with class components, enabling you to initialize, protect and access MSAL React context in your class components.
16
+
17
+
It's important to note that you won't be able to use `@azure/msal-react` hooks inside your class components. If you need access to authentication state inside your class component, you'll need to use `@azure/msal-browser` directly to obtain similar functionality.
18
+
19
+
## Prerequisites
20
+
16
21
17
-
For a working example, see [react-router-sample](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-react-samples/react-router-sample).
18
22
19
23
## Initialization
20
24
21
-
Just like when using function components you will need an [`MsalProvider`](/javascript/api/@azure/msal-react/#@azure-msal-react-msalprovider) component at the top level of the component tree that requires access to authentication state.
25
+
Initialization with class components with MSAL React works very similarly to to function components. Similar to when using function components, you'll need an [`MsalProvider`](/javascript/api/@azure/msal-react/#@azure-msal-react-msalprovider) component at the top level of the component tree that requires access to authentication state.
26
+
27
+
In the following snippet, the `MsalProvider` component is used to wrap the entire application, making the MSAL instance available to all child components. This enables child components to use MSAL for user authentication, acquiring tokens, and calling protected APIs.
22
28
23
29
```javascript
24
30
importReactfrom"react";
@@ -40,7 +46,13 @@ class App extends React.Component {
40
46
41
47
## Protecting Components
42
48
43
-
Just like when using function components you can use the `AuthenticatedTemplate`, `UnauthenticatedTemplate` and `MsalAuthenticationTemplate` to conditionally render your components.
49
+
In MSAL React, you can protect your components and conditionally render then based on the authentication state of the user. This works similarly to using function components. The main examples are:
50
+
51
+
*[`AuthenticatedTemplate`](/javascript/api/@azure/msal-react/#@azure-msal-react-authenticatedtemplate) - This component will only render its children if the user is authenticated. If the user is not authenticated, it will not render anything.
52
+
*[`UnauthenticatedTemplate`](/javascript/api/@azure/msal-react/#@azure-msal-react-unauthenticatedtemplate) - This component will only render its children if the user is not authenticated. If the user is authenticated, it will not render anything.
53
+
*[`MsalAuthenticationTemplate`](/javascript/api/@azure/msal-react/#@azure-msal-react-msalauthenticationtemplate) - This component attempts to authenticate the user before rendering its children. You can specify the interaction type (redirect or popup) as a prop. If the user is not authenticated, it will initiate the authentication process.
54
+
55
+
The following snippet shows how to use `AuthenticatedTemplate`, `UnauthenticatedTemplate`, and `MsalAuthenticationTemplate` to protect your React components. Note how `MSAL Provider` wraps around the child components.
44
56
45
57
```javascript
46
58
importReactfrom"react";
@@ -70,10 +82,12 @@ class App extends React.Component {
70
82
71
83
## Accessing MSAL React context in a class component
72
84
73
-
Since you can't use the `useMsal` hook to access the MSAL React context in a class component you have 2 other options. You can either use the raw context directly or you can use the `withMsal` higher order component to inject the context into your component's props. <!--IMsalContext?-->
85
+
The `useMsal` hook can't be used to access the MSAL React context in a class component. This is because hooks can only be used in function components where you can use features without an instance. Because class components have instances, you have 2 other options. You can either use the raw context directly or you can use the `withMsal` higher order component to inject the context into your component's props. <!--IMsalContext?-->
74
86
75
87
### Accessing raw context
76
88
89
+
The following snippet shows how to use the `MsalContext` to access the raw context in a class component.
@@ -103,10 +117,12 @@ class YourClassComponent extends React.Component {
103
117
}
104
118
```
105
119
106
-
For a working example, see [ProfileRawContext.jsx](../../../samples/msal-react-samples/react-router-sample) in [react-router-sample](../../../samples/msal-react-samples/react-router-sample/src/pages/ProfileRawContext.jsx).
120
+
For a working example, refer to [*ProfileRawContext.jsx*](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-react-samples/react-router-sample/src/pages/ProfileRawContext.jsx) in our [react-router-sample](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-react-samples/react-router-sample).
107
121
108
122
### Accessing via withMsal HOC
109
123
124
+
The alternative is to use the `withMsal` higher order component to inject the context into your component's props. The following snippet shows how to use the `withMsal` HOC to access the MSAL React context in a class component.
@@ -136,18 +152,21 @@ class App extends React.Component {
136
152
}
137
153
```
138
154
139
-
For a working example, see [ProfileWithMsal.jsx](../../../samples/msal-react-samples/react-router-sample) in [react-router-sample](../../../samples/msal-react-samples/react-router-sample/src/pages/ProfileWithMsal.jsx).
155
+
For a working example, refer to [*ProfileWithMsal.jsx*](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-react-samples/react-router-sample/src/pages/ProfileWithMsal.jsx) in [react-router-sample](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-react-samples/react-router-sample).
140
156
141
157
## Logging in using a class component
142
158
143
-
Regardless of which approach you take to get the `MSAL React` context the usage will be the same. Once you have the context object you can invoke [any of the APIs](https://azuread.github.io/microsoft-authentication-library-for-js/ref/interfaces/_azure_msal_browser.ipublicclientapplication.html) on `PublicClientApplication`, inspect the accounts signed in, or determine if authentication is currently in progress.
159
+
Regardless of which approach you take to get the `MSAL React` context, the usage will be the same. Once you have the context object you can invoke [any of the APIs](/javascript/api/@azure/msal-browser/publicclientapplication) on `PublicClientApplication`, inspect the accounts signed in, or determine if authentication is currently in progress.
144
160
145
161
The following examples will show how to login using the `withMsal` HOC approach but you can quickly adapt to the other approach if needed.
146
162
147
-
**Note**: By now you should be aware that an `MsalProvider` component must be rendered at any level above any component that uses context, the examples below will assume there is a provider and will not demonstrate this.
163
+
> [!NOTE]
164
+
> It's important to remember that an `MsalProvider` component must be rendered at any level above any component that uses context. The following examples assume there is a provider and won't demonstrate this.
148
165
149
166
### Logging in as a result of clicking a button
150
167
168
+
The following snippet defines a React class component `LoginButton` that uses the `withMsal` higher-order component. It renders a button that either triggers a login popup if the user is not authenticated, or logs out the user if they are authenticated.
The following snippet defines a React class component `ProtectedComponent` that uses the `withMsal` higher-order component. It attempts to authenticate the user upon mounting and updating, and displays whether the user is authenticated or if authentication is in progress on the loading page.
192
+
172
193
```javascript
173
194
importReactfrom"react";
174
195
import { withMsal } from"@azure/msal-react";
@@ -204,3 +225,7 @@ class ProtectedComponent extends React.Component {
- Check out our [react-router-sample](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-react-samples/react-router-sample).
Copy file name to clipboardExpand all lines: msal-javascript-conceptual/react/errors.md
+20-28Lines changed: 20 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: Handle errors and exceptions in MSAL.js
2
+
title: Handle errors and exceptions in MSAL Node
3
3
description: Learn how to handle use MSAL React with class components. covering initialization, protecting components, accessing MSAL React context and logging in.
This article covers how to handle some of the errors and exceptions that can be thrown by MSAL React. We'll cover BrowserConfigurationAuthErrors, and BrowserAuthErrors and provide some troubleshooting steps to help you resolve them.
28
16
29
17
## BrowserConfigurationAuthErrors
30
18
31
-
### stubbed_public_client_application_called
19
+
BrowserConfigurationAuthErrors are used to represent errors that occur due to incorrect configuration of the MSAL authentication parameters, and can occur when initializing a `PublicClientApplication` or `ConfidentialClientApplication` instance. If the configuration object passed to the constructor does not meet the library's requirements, you don't provide a valid client ID or authority, or if you provide an invalid redirect URI, you might encounter this type of error
32
20
33
-
**Error Message**: Stub instance of Public Client Application was called. If using `@azure/msal-react`, please ensure context is not used without a provider.
21
+
### `stubbed_public_client_application_called`
34
22
35
-
When using `@azure/msal-react` this error is thrown when you try to use an msal component or hook without an `MsalProvider` higher up in the component tree. All hooks and components make use of the [React Context API](https://reactjs.org/docs/context.html) and require a provider.
23
+
This error means that a method has been called on a `PublicClientApplication` instance that has been stubbed out or incorrectly initialized, when you try to use an msal component or hook without an `MsalProvider` higher up in the component tree. All hooks and components make use of the [React Context API](https://reactjs.org/docs/context.html) and require a provider. You may get a message similar to `Stub instance of PublicClientApplication was called. If using @azure/msal-react, please ensure context is not used without a provider`.
36
24
37
-
❌ The following example will throw this error because the `useMsal` hook is used outside the context of `MsalProvider`:
25
+
The following snippet will throw this error because the `useMsal` hook is used outside the context of `MsalProvider`.
✔️ To resolve the error you should refactor the code above so that the `useMsal` hook is called in a component underneath `MsalProvider`:
44
+
To resolve the error you should refactor the preceding snippet so that the `useMsal` hook is called in a component underneath `MsalProvider`. The correct implementation is shown in the following snippet.
BrowserAuthErrors are used to represent errors that occur during the authentication process in a browser environment. Such occurrences can be when a user tries to initiate a new login request while a previous request is still being processed. To resolve these errors, you should ensure that each interaction has completed before starting a new one.
82
70
83
-
**Error Message**: Interaction is currently in progress. Please ensure that this interaction has been completed before calling an interactive API.
71
+
### `Interaction_in_progress`
84
72
85
-
This error is thrown when an interactive API (`loginPopup`, `loginRedirect`, `acquireTokenPopup`, `acquireTokenRedirect`) is invoked while another interactive API is still in progress. The login and acquireToken APIs are async so you will need to ensure that the resulting promises have resolved before invoking another one.
73
+
This error is thrown when an interactive API (`loginPopup`, `loginRedirect`, `acquireTokenPopup`, `acquireTokenRedirect`) is invoked while another interactive API is still in progress. The login and acquireToken APIs are async so you will need to ensure that the resulting promises have resolved before invoking another one. You may get an error message similar to `Interaction is currently in progress. Please ensure that this interaction has been completed before calling an interactive API.`
86
74
87
75
In `@azure/msal-react` there are 2 scenarios when this can happen:
88
76
89
-
1. Your application is calling one of the above APIs outside of the context where you do not have access to the `inProgress` state. For more about context see the [FAQ](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-react/FAQ.md#what-can-i-do-outside-of-msal-react-context)
90
-
1. Your application is calling one of the above APIs without first checking if interaction is already in progress elsewhere.
77
+
1. Your application is calling one of the APIs outside of the context where you do not have access to the `inProgress` state. For more about context see the [FAQ](./faq.md#what-can-i-do-outside-of-msal-react-context)
78
+
1. Your application is calling one of the APIs without first checking if interaction is already in progress elsewhere.
91
79
92
-
❌ The following example will throw this error when another component has already invoked an interactive API that is in progress:
80
+
The following snippet throws the error when another component has already invoked an interactive API that is in progress:
@@ -129,11 +117,15 @@ export function exampleComponent() {
129
117
130
118
#### Troubleshooting Steps
131
119
132
-
-[Enable verbose logging](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/configuration.md#using-the-config-object) and trace the order of events. Verify that an interactive API is not invoked before another has resolved. If using the redirect flow make sure `handleRedirectPromise` has resolved (done in the `MsalProvider`).
120
+
-[Enable verbose logging](../browser/configuration.md#using-the-config-object) and trace the order of events. Verify that an interactive API is not invoked before another has resolved. If using the redirect flow make sure `handleRedirectPromise` has resolved (done in the `MsalProvider`).
133
121
134
122
If you are unable to figure out why this error is being thrown please [open an issue](https://github.com/AzureAD/microsoft-authentication-library-for-js/issues/new/choose) and be prepared to share the following information:
135
123
136
124
- Verbose logs
137
125
- A sample app and/or code snippets that we can use to reproduce the issue
138
126
- Refresh the page. Does the error go away?
139
127
- Open your application in a new tab. Does the error go away?
128
+
129
+
## See also
130
+
131
+
There are additional errors that can be thrown by MSAL.js. You can refer to the [full list of errors](../browser/errors.md) covered in MSAL Browser.
0 commit comments