@azure/msal-react 3.0.22 → 5.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/dist/MsalContext.js +1 -1
- package/dist/MsalProvider.js +5 -5
- package/dist/MsalProvider.js.map +1 -1
- package/dist/components/AuthenticatedTemplate.js +1 -1
- package/dist/components/MsalAuthenticationTemplate.js +1 -1
- package/dist/components/UnauthenticatedTemplate.js +1 -1
- package/dist/components/withMsal.js +1 -1
- package/dist/error/ReactAuthError.js +1 -1
- package/dist/hooks/useAccount.d.ts +6 -0
- package/dist/hooks/useAccount.js +50 -13
- package/dist/hooks/useAccount.js.map +1 -1
- package/dist/hooks/useIsAuthenticated.js +1 -1
- package/dist/hooks/useMsal.js +1 -1
- package/dist/hooks/useMsalAuthentication.js +69 -72
- package/dist/hooks/useMsalAuthentication.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/packageMetadata.d.ts +1 -1
- package/dist/packageMetadata.js +2 -2
- package/dist/tsdoc-metadata.json +1 -1
- package/dist/utils/utilities.js +1 -1
- package/lib/msal-react.cjs +119 -85
- package/lib/msal-react.cjs.map +1 -1
- package/lib/types/hooks/useAccount.d.ts +6 -0
- package/lib/types/packageMetadata.d.ts +1 -1
- package/package.json +6 -8
- package/src/MsalProvider.tsx +8 -4
- package/src/hooks/useAccount.ts +65 -15
- package/src/hooks/useMsalAuthentication.ts +106 -95
- package/src/packageMetadata.ts +1 -1
|
@@ -11,12 +11,11 @@ import {
|
|
|
11
11
|
InteractionType,
|
|
12
12
|
AuthenticationResult,
|
|
13
13
|
AuthError,
|
|
14
|
-
EventMessage,
|
|
15
|
-
EventType,
|
|
16
14
|
InteractionStatus,
|
|
17
15
|
SilentRequest,
|
|
18
16
|
InteractionRequiredAuthError,
|
|
19
17
|
OIDC_DEFAULT_SCOPES,
|
|
18
|
+
BrowserUtils,
|
|
20
19
|
} from "@azure/msal-browser";
|
|
21
20
|
import { useIsAuthenticated } from "./useIsAuthenticated.js";
|
|
22
21
|
import { AccountIdentifiers } from "../types/AccountIdentifiers.js";
|
|
@@ -95,26 +94,66 @@ export function useMsalAuthentication(
|
|
|
95
94
|
): Promise<AuthenticationResult | null> => {
|
|
96
95
|
const loginType = callbackInteractionType || interactionType;
|
|
97
96
|
const loginRequest = callbackRequest || authenticationRequest;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
.
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
97
|
+
|
|
98
|
+
const getToken = async (): Promise<AuthenticationResult | null> => {
|
|
99
|
+
logger.verbose(
|
|
100
|
+
"useMsalAuthentication - Calling getToken",
|
|
101
|
+
callbackRequest?.correlationId || ""
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
switch (loginType) {
|
|
105
|
+
case InteractionType.Popup:
|
|
106
|
+
logger.verbose(
|
|
107
|
+
"useMsalAuthentication - Calling loginPopup",
|
|
108
|
+
callbackRequest?.correlationId || ""
|
|
109
|
+
);
|
|
110
|
+
return instance.loginPopup(
|
|
111
|
+
loginRequest as PopupRequest
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
case InteractionType.Redirect:
|
|
115
|
+
// This promise is not expected to resolve due to full frame redirect
|
|
116
|
+
logger.verbose(
|
|
117
|
+
"useMsalAuthentication - Calling loginRedirect",
|
|
118
|
+
callbackRequest?.correlationId || ""
|
|
119
|
+
);
|
|
120
|
+
await instance.handleRedirectPromise();
|
|
121
|
+
return instance
|
|
122
|
+
.loginRedirect(loginRequest as RedirectRequest)
|
|
123
|
+
.then(() => null);
|
|
124
|
+
|
|
125
|
+
case InteractionType.Silent:
|
|
126
|
+
logger.verbose(
|
|
127
|
+
"useMsalAuthentication - Calling ssoSilent",
|
|
128
|
+
callbackRequest?.correlationId || ""
|
|
129
|
+
);
|
|
130
|
+
return instance.ssoSilent(
|
|
131
|
+
loginRequest as SsoSilentRequest
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
default:
|
|
135
|
+
const invalidTypeError =
|
|
136
|
+
ReactAuthError.createInvalidInteractionTypeError();
|
|
137
|
+
if (mounted.current) {
|
|
138
|
+
setResponse([null, invalidTypeError]);
|
|
139
|
+
}
|
|
140
|
+
throw invalidTypeError;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
return getToken()
|
|
145
|
+
.then((response: AuthenticationResult | null) => {
|
|
146
|
+
if (response && mounted.current) {
|
|
147
|
+
setResponse([response, null]);
|
|
148
|
+
}
|
|
149
|
+
return response;
|
|
150
|
+
})
|
|
151
|
+
.catch((e: AuthError) => {
|
|
152
|
+
if (mounted.current) {
|
|
153
|
+
setResponse([null, e]);
|
|
154
|
+
}
|
|
155
|
+
throw e;
|
|
156
|
+
});
|
|
118
157
|
},
|
|
119
158
|
[instance, interactionType, authenticationRequest, logger]
|
|
120
159
|
);
|
|
@@ -131,14 +170,16 @@ export function useMsalAuthentication(
|
|
|
131
170
|
|
|
132
171
|
if (callbackRequest) {
|
|
133
172
|
logger.trace(
|
|
134
|
-
"useMsalAuthentication - acquireToken - Using request provided in the callback"
|
|
173
|
+
"useMsalAuthentication - acquireToken - Using request provided in the callback",
|
|
174
|
+
callbackRequest.correlationId || ""
|
|
135
175
|
);
|
|
136
176
|
tokenRequest = {
|
|
137
177
|
...callbackRequest,
|
|
138
178
|
};
|
|
139
179
|
} else if (authenticationRequest) {
|
|
140
180
|
logger.trace(
|
|
141
|
-
"useMsalAuthentication - acquireToken - Using request provided in the hook"
|
|
181
|
+
"useMsalAuthentication - acquireToken - Using request provided in the hook",
|
|
182
|
+
authenticationRequest.correlationId || ""
|
|
142
183
|
);
|
|
143
184
|
tokenRequest = {
|
|
144
185
|
...authenticationRequest,
|
|
@@ -146,56 +187,57 @@ export function useMsalAuthentication(
|
|
|
146
187
|
};
|
|
147
188
|
} else {
|
|
148
189
|
logger.trace(
|
|
149
|
-
"useMsalAuthentication - acquireToken - No request object provided, using default request."
|
|
190
|
+
"useMsalAuthentication - acquireToken - No request object provided, using default request.",
|
|
191
|
+
""
|
|
150
192
|
);
|
|
151
193
|
tokenRequest = {
|
|
152
194
|
scopes: OIDC_DEFAULT_SCOPES,
|
|
153
195
|
};
|
|
154
196
|
}
|
|
155
197
|
|
|
198
|
+
const correlationId =
|
|
199
|
+
tokenRequest.correlationId || BrowserUtils.createGuid();
|
|
200
|
+
tokenRequest.correlationId = correlationId;
|
|
201
|
+
|
|
156
202
|
if (!tokenRequest.account && account) {
|
|
157
203
|
logger.trace(
|
|
158
|
-
"useMsalAuthentication - acquireToken - Attaching account to request"
|
|
204
|
+
"useMsalAuthentication - acquireToken - Attaching account to request",
|
|
205
|
+
correlationId
|
|
159
206
|
);
|
|
160
207
|
tokenRequest.account = account;
|
|
161
208
|
}
|
|
162
209
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
);
|
|
167
|
-
return instance
|
|
168
|
-
.acquireTokenSilent(tokenRequest)
|
|
169
|
-
.catch(async (e: AuthError) => {
|
|
170
|
-
if (e instanceof InteractionRequiredAuthError) {
|
|
171
|
-
if (!interactionInProgress.current) {
|
|
172
|
-
logger.error(
|
|
173
|
-
"useMsalAuthentication - Interaction required, falling back to interaction"
|
|
174
|
-
);
|
|
175
|
-
return login(
|
|
176
|
-
fallbackInteractionType,
|
|
177
|
-
tokenRequest
|
|
178
|
-
);
|
|
179
|
-
} else {
|
|
180
|
-
logger.error(
|
|
181
|
-
"useMsalAuthentication - Interaction required but is already in progress. Please try again, if needed, after interaction completes."
|
|
182
|
-
);
|
|
183
|
-
throw ReactAuthError.createUnableToFallbackToInteractionError();
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
throw e;
|
|
188
|
-
});
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
return getToken()
|
|
192
|
-
.then((response: AuthenticationResult | null) => {
|
|
210
|
+
return instance
|
|
211
|
+
.acquireTokenSilent(tokenRequest)
|
|
212
|
+
.then((response: AuthenticationResult) => {
|
|
193
213
|
if (mounted.current) {
|
|
194
214
|
setResponse([response, null]);
|
|
195
215
|
}
|
|
196
216
|
return response;
|
|
197
217
|
})
|
|
198
|
-
.catch((e: AuthError) => {
|
|
218
|
+
.catch(async (e: AuthError) => {
|
|
219
|
+
if (e instanceof InteractionRequiredAuthError) {
|
|
220
|
+
if (!interactionInProgress.current) {
|
|
221
|
+
logger.error(
|
|
222
|
+
"useMsalAuthentication - Interaction required, falling back to interaction",
|
|
223
|
+
correlationId
|
|
224
|
+
);
|
|
225
|
+
return login(fallbackInteractionType, tokenRequest);
|
|
226
|
+
} else {
|
|
227
|
+
const fallbackError =
|
|
228
|
+
ReactAuthError.createUnableToFallbackToInteractionError();
|
|
229
|
+
logger.error(
|
|
230
|
+
"useMsalAuthentication - Interaction required but is already in progress. Please try again, if needed, after interaction completes.",
|
|
231
|
+
correlationId
|
|
232
|
+
);
|
|
233
|
+
if (mounted.current) {
|
|
234
|
+
setResponse([null, fallbackError]);
|
|
235
|
+
}
|
|
236
|
+
throw fallbackError;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Handle other errors
|
|
199
241
|
if (mounted.current) {
|
|
200
242
|
setResponse([null, e]);
|
|
201
243
|
}
|
|
@@ -212,42 +254,6 @@ export function useMsalAuthentication(
|
|
|
212
254
|
]
|
|
213
255
|
);
|
|
214
256
|
|
|
215
|
-
useEffect(() => {
|
|
216
|
-
const callbackId = instance.addEventCallback(
|
|
217
|
-
(message: EventMessage) => {
|
|
218
|
-
switch (message.eventType) {
|
|
219
|
-
case EventType.LOGIN_SUCCESS:
|
|
220
|
-
case EventType.SSO_SILENT_SUCCESS:
|
|
221
|
-
if (message.payload) {
|
|
222
|
-
setResponse([
|
|
223
|
-
message.payload as AuthenticationResult,
|
|
224
|
-
null,
|
|
225
|
-
]);
|
|
226
|
-
}
|
|
227
|
-
break;
|
|
228
|
-
case EventType.LOGIN_FAILURE:
|
|
229
|
-
case EventType.SSO_SILENT_FAILURE:
|
|
230
|
-
if (message.error) {
|
|
231
|
-
setResponse([null, message.error as AuthError]);
|
|
232
|
-
}
|
|
233
|
-
break;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
);
|
|
237
|
-
logger.verbose(
|
|
238
|
-
`useMsalAuthentication - Registered event callback with id: ${callbackId}`
|
|
239
|
-
);
|
|
240
|
-
|
|
241
|
-
return () => {
|
|
242
|
-
if (callbackId) {
|
|
243
|
-
logger.verbose(
|
|
244
|
-
`useMsalAuthentication - Removing event callback ${callbackId}`
|
|
245
|
-
);
|
|
246
|
-
instance.removeEventCallback(callbackId);
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
}, [instance, logger]);
|
|
250
|
-
|
|
251
257
|
useEffect(() => {
|
|
252
258
|
if (
|
|
253
259
|
shouldAcquireToken.current &&
|
|
@@ -256,7 +262,8 @@ export function useMsalAuthentication(
|
|
|
256
262
|
if (!isAuthenticated) {
|
|
257
263
|
shouldAcquireToken.current = false;
|
|
258
264
|
logger.info(
|
|
259
|
-
"useMsalAuthentication - No user is authenticated, attempting to login"
|
|
265
|
+
"useMsalAuthentication - No user is authenticated, attempting to login",
|
|
266
|
+
""
|
|
260
267
|
);
|
|
261
268
|
login().catch(() => {
|
|
262
269
|
// Errors are saved in state above
|
|
@@ -265,13 +272,17 @@ export function useMsalAuthentication(
|
|
|
265
272
|
} else if (account) {
|
|
266
273
|
shouldAcquireToken.current = false;
|
|
267
274
|
logger.info(
|
|
268
|
-
"useMsalAuthentication - User is authenticated, attempting to acquire token"
|
|
275
|
+
"useMsalAuthentication - User is authenticated, attempting to acquire token",
|
|
276
|
+
""
|
|
269
277
|
);
|
|
270
278
|
acquireToken().catch(() => {
|
|
271
279
|
// Errors are saved in state above
|
|
272
280
|
return;
|
|
273
281
|
});
|
|
274
282
|
}
|
|
283
|
+
// If logging out, set the state to null so the component doesn't display authenticated content for a user that is logged out
|
|
284
|
+
} else if (!account && result) {
|
|
285
|
+
setResponse([null, null]);
|
|
275
286
|
}
|
|
276
287
|
}, [isAuthenticated, account, inProgress, login, acquireToken, logger]);
|
|
277
288
|
|
package/src/packageMetadata.ts
CHANGED