@azure/msal-react 2.0.0-alpha.1 → 2.0.0-beta.0

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.
Files changed (39) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +144 -144
  3. package/dist/MsalContext.d.ts +10 -10
  4. package/dist/MsalContext.js +16 -16
  5. package/dist/MsalProvider.d.ts +9 -9
  6. package/dist/MsalProvider.js +130 -131
  7. package/dist/MsalProvider.js.map +1 -1
  8. package/dist/components/AuthenticatedTemplate.d.ts +8 -8
  9. package/dist/components/AuthenticatedTemplate.js +23 -23
  10. package/dist/components/MsalAuthenticationTemplate.d.ts +16 -16
  11. package/dist/components/MsalAuthenticationTemplate.js +33 -33
  12. package/dist/components/UnauthenticatedTemplate.d.ts +8 -8
  13. package/dist/components/UnauthenticatedTemplate.js +25 -25
  14. package/dist/components/withMsal.d.ts +11 -11
  15. package/dist/components/withMsal.js +17 -17
  16. package/dist/error/ReactAuthError.d.ts +16 -16
  17. package/dist/error/ReactAuthError.js +27 -27
  18. package/dist/hooks/useAccount.d.ts +7 -7
  19. package/dist/hooks/useAccount.js +33 -33
  20. package/dist/hooks/useIsAuthenticated.d.ts +6 -6
  21. package/dist/hooks/useIsAuthenticated.js +30 -30
  22. package/dist/hooks/useMsal.d.ts +5 -5
  23. package/dist/hooks/useMsal.js +8 -8
  24. package/dist/hooks/useMsalAuthentication.d.ts +18 -18
  25. package/dist/hooks/useMsalAuthentication.js +184 -184
  26. package/dist/index.d.ts +23 -23
  27. package/dist/index.js +1 -1
  28. package/dist/packageMetadata.d.ts +2 -2
  29. package/dist/packageMetadata.js +4 -4
  30. package/dist/types/AccountIdentifiers.d.ts +2 -2
  31. package/dist/utils/utilities.d.ts +17 -17
  32. package/dist/utils/utilities.js +60 -60
  33. package/package.json +69 -65
  34. package/dist/msal-react.cjs.development.js +0 -685
  35. package/dist/msal-react.cjs.development.js.map +0 -1
  36. package/dist/msal-react.cjs.production.min.js +0 -2
  37. package/dist/msal-react.cjs.production.min.js.map +0 -1
  38. package/dist/msal-react.esm.js +0 -667
  39. package/dist/msal-react.esm.js.map +0 -1
@@ -1,685 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
7
- var React = require('react');
8
- var React__default = _interopDefault(React);
9
- var msalBrowser = require('@azure/msal-browser');
10
-
11
- /*
12
- * Copyright (c) Microsoft Corporation. All rights reserved.
13
- * Licensed under the MIT License.
14
- */
15
- /*
16
- * Stubbed context implementation
17
- * Only used when there is no provider, which is an unsupported scenario
18
- */
19
-
20
- const defaultMsalContext = {
21
- instance: msalBrowser.stubbedPublicClientApplication,
22
- inProgress: msalBrowser.InteractionStatus.None,
23
- accounts: [],
24
- logger: /*#__PURE__*/new msalBrowser.Logger({})
25
- };
26
- const MsalContext = /*#__PURE__*/React.createContext(defaultMsalContext);
27
- const MsalConsumer = MsalContext.Consumer;
28
-
29
- /*
30
- * Copyright (c) Microsoft Corporation. All rights reserved.
31
- * Licensed under the MIT License.
32
- */
33
- function getChildrenOrFunction(children, args) {
34
- if (typeof children === "function") {
35
- return children(args);
36
- }
37
-
38
- return children;
39
- }
40
- /**
41
- * Helper function to determine whether 2 arrays are equal
42
- * Used to avoid unnecessary state updates
43
- * @param arrayA
44
- * @param arrayB
45
- */
46
-
47
- function accountArraysAreEqual(arrayA, arrayB) {
48
- if (arrayA.length !== arrayB.length) {
49
- return false;
50
- }
51
-
52
- const comparisonArray = [...arrayB];
53
- return arrayA.every(elementA => {
54
- const elementB = comparisonArray.shift();
55
-
56
- if (!elementA || !elementB) {
57
- return false;
58
- }
59
-
60
- return elementA.homeAccountId === elementB.homeAccountId && elementA.localAccountId === elementB.localAccountId && elementA.username === elementB.username;
61
- });
62
- }
63
- function getAccountByIdentifiers(allAccounts, accountIdentifiers) {
64
- if (allAccounts.length > 0 && (accountIdentifiers.homeAccountId || accountIdentifiers.localAccountId || accountIdentifiers.username)) {
65
- const matchedAccounts = allAccounts.filter(accountObj => {
66
- if (accountIdentifiers.username && accountIdentifiers.username.toLowerCase() !== accountObj.username.toLowerCase()) {
67
- return false;
68
- }
69
-
70
- if (accountIdentifiers.homeAccountId && accountIdentifiers.homeAccountId.toLowerCase() !== accountObj.homeAccountId.toLowerCase()) {
71
- return false;
72
- }
73
-
74
- if (accountIdentifiers.localAccountId && accountIdentifiers.localAccountId.toLowerCase() !== accountObj.localAccountId.toLowerCase()) {
75
- return false;
76
- }
77
-
78
- return true;
79
- });
80
- return matchedAccounts[0] || null;
81
- } else {
82
- return null;
83
- }
84
- }
85
-
86
- /* eslint-disable header/header */
87
- const name = "@azure/msal-react";
88
- const version = "1.5.7";
89
-
90
- /*
91
- * Copyright (c) Microsoft Corporation. All rights reserved.
92
- * Licensed under the MIT License.
93
- */
94
- var MsalProviderActionType;
95
-
96
- (function (MsalProviderActionType) {
97
- MsalProviderActionType["UNBLOCK_INPROGRESS"] = "UNBLOCK_INPROGRESS";
98
- MsalProviderActionType["EVENT"] = "EVENT";
99
- })(MsalProviderActionType || (MsalProviderActionType = {}));
100
- /**
101
- * Returns the next inProgress and accounts state based on event message
102
- * @param previousState
103
- * @param action
104
- */
105
-
106
-
107
- const reducer = (previousState, action) => {
108
- const {
109
- type,
110
- payload
111
- } = action;
112
- let newInProgress = previousState.inProgress;
113
-
114
- switch (type) {
115
- case MsalProviderActionType.UNBLOCK_INPROGRESS:
116
- if (previousState.inProgress === msalBrowser.InteractionStatus.Startup) {
117
- newInProgress = msalBrowser.InteractionStatus.None;
118
- payload.logger.info("MsalProvider - handleRedirectPromise resolved, setting inProgress to 'none'");
119
- }
120
-
121
- break;
122
-
123
- case MsalProviderActionType.EVENT:
124
- const message = payload.message;
125
- const status = msalBrowser.EventMessageUtils.getInteractionStatusFromEvent(message, previousState.inProgress);
126
-
127
- if (status) {
128
- payload.logger.info(`MsalProvider - ${message.eventType} results in setting inProgress from ${previousState.inProgress} to ${status}`);
129
- newInProgress = status;
130
- }
131
-
132
- break;
133
-
134
- default:
135
- throw new Error(`Unknown action type: ${type}`);
136
- }
137
-
138
- const currentAccounts = payload.instance.getAllAccounts();
139
-
140
- if (newInProgress !== previousState.inProgress && !accountArraysAreEqual(currentAccounts, previousState.accounts)) {
141
- // Both inProgress and accounts changed
142
- return { ...previousState,
143
- inProgress: newInProgress,
144
- accounts: currentAccounts
145
- };
146
- } else if (newInProgress !== previousState.inProgress) {
147
- // Only only inProgress changed
148
- return { ...previousState,
149
- inProgress: newInProgress
150
- };
151
- } else if (!accountArraysAreEqual(currentAccounts, previousState.accounts)) {
152
- // Only accounts changed
153
- return { ...previousState,
154
- accounts: currentAccounts
155
- };
156
- } else {
157
- // Nothing changed
158
- return previousState;
159
- }
160
- };
161
- /**
162
- * MSAL context provider component. This must be rendered above any other components that use MSAL.
163
- */
164
-
165
-
166
- function MsalProvider(_ref) {
167
- let {
168
- instance,
169
- children
170
- } = _ref;
171
- React.useEffect(() => {
172
- instance.initializeWrapperLibrary(msalBrowser.WrapperSKU.React, version);
173
- }, [instance]); // Create a logger instance for msal-react with the same options as PublicClientApplication
174
-
175
- const logger = React.useMemo(() => {
176
- return instance.getLogger().clone(name, version);
177
- }, [instance]);
178
- const [state, updateState] = React.useReducer(reducer, undefined, () => {
179
- // Lazy initialization of the initial state
180
- return {
181
- inProgress: msalBrowser.InteractionStatus.Startup,
182
- accounts: instance.getAllAccounts()
183
- };
184
- });
185
- React.useEffect(() => {
186
- const callbackId = instance.addEventCallback(message => {
187
- updateState({
188
- payload: {
189
- instance,
190
- logger,
191
- message
192
- },
193
- type: MsalProviderActionType.EVENT
194
- });
195
- });
196
- logger.verbose(`MsalProvider - Registered event callback with id: ${callbackId}`);
197
- instance.initialize().then(() => {
198
- instance.handleRedirectPromise().catch(() => {
199
- // Errors should be handled by listening to the LOGIN_FAILURE event
200
- return;
201
- }).finally(() => {
202
- /*
203
- * If handleRedirectPromise returns a cached promise the necessary events may not be fired
204
- * This is a fallback to prevent inProgress from getting stuck in 'startup'
205
- */
206
- updateState({
207
- payload: {
208
- instance,
209
- logger
210
- },
211
- type: MsalProviderActionType.UNBLOCK_INPROGRESS
212
- });
213
- });
214
- });
215
- return () => {
216
- // Remove callback when component unmounts or accounts change
217
- if (callbackId) {
218
- logger.verbose(`MsalProvider - Removing event callback ${callbackId}`);
219
- instance.removeEventCallback(callbackId);
220
- }
221
- };
222
- }, [instance, logger]);
223
- const contextValue = {
224
- instance,
225
- inProgress: state.inProgress,
226
- accounts: state.accounts,
227
- logger
228
- };
229
- return React__default.createElement(MsalContext.Provider, {
230
- value: contextValue
231
- }, children);
232
- }
233
-
234
- /*
235
- * Copyright (c) Microsoft Corporation. All rights reserved.
236
- * Licensed under the MIT License.
237
- */
238
- /**
239
- * Returns Msal Context values
240
- */
241
-
242
- const useMsal = () => React.useContext(MsalContext);
243
-
244
- /*
245
- * Copyright (c) Microsoft Corporation. All rights reserved.
246
- * Licensed under the MIT License.
247
- */
248
-
249
- function isAuthenticated(allAccounts, matchAccount) {
250
- if (matchAccount && (matchAccount.username || matchAccount.homeAccountId || matchAccount.localAccountId)) {
251
- return !!getAccountByIdentifiers(allAccounts, matchAccount);
252
- }
253
-
254
- return allAccounts.length > 0;
255
- }
256
- /**
257
- * Returns whether or not a user is currently signed-in. Optionally provide 1 or more accountIdentifiers to determine if a specific user is signed-in
258
- * @param matchAccount
259
- */
260
-
261
-
262
- function useIsAuthenticated(matchAccount) {
263
- const {
264
- accounts: allAccounts,
265
- inProgress
266
- } = useMsal();
267
- const [hasAuthenticated, setHasAuthenticated] = React.useState(() => {
268
- if (inProgress === msalBrowser.InteractionStatus.Startup) {
269
- return false;
270
- }
271
-
272
- return isAuthenticated(allAccounts, matchAccount);
273
- });
274
- React.useEffect(() => {
275
- setHasAuthenticated(isAuthenticated(allAccounts, matchAccount));
276
- }, [allAccounts, matchAccount]);
277
- return hasAuthenticated;
278
- }
279
-
280
- /*
281
- * Copyright (c) Microsoft Corporation. All rights reserved.
282
- * Licensed under the MIT License.
283
- */
284
- /**
285
- * Renders child components if user is authenticated
286
- * @param props
287
- */
288
-
289
- function AuthenticatedTemplate(_ref) {
290
- let {
291
- username,
292
- homeAccountId,
293
- localAccountId,
294
- children
295
- } = _ref;
296
- const context = useMsal();
297
- const accountIdentifier = React.useMemo(() => {
298
- return {
299
- username,
300
- homeAccountId,
301
- localAccountId
302
- };
303
- }, [username, homeAccountId, localAccountId]);
304
- const isAuthenticated = useIsAuthenticated(accountIdentifier);
305
-
306
- if (isAuthenticated && context.inProgress !== msalBrowser.InteractionStatus.Startup) {
307
- return React__default.createElement(React__default.Fragment, null, getChildrenOrFunction(children, context));
308
- }
309
-
310
- return null;
311
- }
312
-
313
- /*
314
- * Copyright (c) Microsoft Corporation. All rights reserved.
315
- * Licensed under the MIT License.
316
- */
317
- /**
318
- * Renders child components if user is unauthenticated
319
- * @param props
320
- */
321
-
322
- function UnauthenticatedTemplate(_ref) {
323
- let {
324
- username,
325
- homeAccountId,
326
- localAccountId,
327
- children
328
- } = _ref;
329
- const context = useMsal();
330
- const accountIdentifier = React.useMemo(() => {
331
- return {
332
- username,
333
- homeAccountId,
334
- localAccountId
335
- };
336
- }, [username, homeAccountId, localAccountId]);
337
- const isAuthenticated = useIsAuthenticated(accountIdentifier);
338
-
339
- if (!isAuthenticated && context.inProgress !== msalBrowser.InteractionStatus.Startup && context.inProgress !== msalBrowser.InteractionStatus.HandleRedirect) {
340
- return React__default.createElement(React__default.Fragment, null, getChildrenOrFunction(children, context));
341
- }
342
-
343
- return null;
344
- }
345
-
346
- /*
347
- * Copyright (c) Microsoft Corporation. All rights reserved.
348
- * Licensed under the MIT License.
349
- */
350
-
351
- function getAccount(instance, accountIdentifiers) {
352
- if (!accountIdentifiers || !accountIdentifiers.homeAccountId && !accountIdentifiers.localAccountId && !accountIdentifiers.username) {
353
- // If no account identifiers are provided, return active account
354
- return instance.getActiveAccount();
355
- }
356
-
357
- return getAccountByIdentifiers(instance.getAllAccounts(), accountIdentifiers);
358
- }
359
- /**
360
- * Given 1 or more accountIdentifiers, returns the Account object if the user is signed-in
361
- * @param accountIdentifiers
362
- */
363
-
364
-
365
- function useAccount(accountIdentifiers) {
366
- const {
367
- instance,
368
- inProgress,
369
- logger
370
- } = useMsal();
371
- const [account, setAccount] = React.useState(() => getAccount(instance, accountIdentifiers));
372
- React.useEffect(() => {
373
- setAccount(currentAccount => {
374
- const nextAccount = getAccount(instance, accountIdentifiers);
375
-
376
- if (!msalBrowser.AccountEntity.accountInfoIsEqual(currentAccount, nextAccount, true)) {
377
- logger.info("useAccount - Updating account");
378
- return nextAccount;
379
- }
380
-
381
- return currentAccount;
382
- });
383
- }, [inProgress, accountIdentifiers, instance, logger]);
384
- return account;
385
- }
386
-
387
- /*
388
- * Copyright (c) Microsoft Corporation. All rights reserved.
389
- * Licensed under the MIT License.
390
- */
391
- const ReactAuthErrorMessage = {
392
- invalidInteractionType: {
393
- code: "invalid_interaction_type",
394
- desc: "The provided interaction type is invalid."
395
- },
396
- unableToFallbackToInteraction: {
397
- code: "unable_to_fallback_to_interaction",
398
- desc: "Interaction is required but another interaction is already in progress. Please try again when the current interaction is complete."
399
- }
400
- };
401
- class ReactAuthError extends msalBrowser.AuthError {
402
- constructor(errorCode, errorMessage) {
403
- super(errorCode, errorMessage);
404
- Object.setPrototypeOf(this, ReactAuthError.prototype);
405
- this.name = "ReactAuthError";
406
- }
407
-
408
- static createInvalidInteractionTypeError() {
409
- return new ReactAuthError(ReactAuthErrorMessage.invalidInteractionType.code, ReactAuthErrorMessage.invalidInteractionType.desc);
410
- }
411
-
412
- static createUnableToFallbackToInteractionError() {
413
- return new ReactAuthError(ReactAuthErrorMessage.unableToFallbackToInteraction.code, ReactAuthErrorMessage.unableToFallbackToInteraction.desc);
414
- }
415
-
416
- }
417
-
418
- /*
419
- * Copyright (c) Microsoft Corporation. All rights reserved.
420
- * Licensed under the MIT License.
421
- */
422
- /**
423
- * If a user is not currently signed in this hook invokes a login. Failed logins can be retried using the login callback returned.
424
- * If a user is currently signed in this hook attempts to acquire a token. Subsequent token requests can use the acquireToken callback returned.
425
- * Optionally provide a request object to be used in the login/acquireToken call.
426
- * Optionally provide a specific user that should be logged in.
427
- * @param interactionType
428
- * @param authenticationRequest
429
- * @param accountIdentifiers
430
- */
431
-
432
- function useMsalAuthentication(interactionType, authenticationRequest, accountIdentifiers) {
433
- const {
434
- instance,
435
- inProgress,
436
- logger
437
- } = useMsal();
438
- const isAuthenticated = useIsAuthenticated(accountIdentifiers);
439
- const account = useAccount(accountIdentifiers);
440
- const [[result, error], setResponse] = React.useState([null, null]); // Used to prevent state updates after unmount
441
-
442
- const mounted = React.useRef(true);
443
- React.useEffect(() => {
444
- return () => {
445
- mounted.current = false;
446
- };
447
- }, []); // Boolean used to check if interaction is in progress in acquireTokenSilent fallback. Use Ref instead of state to prevent acquireToken function from being regenerated on each change to interactionInProgress value
448
-
449
- const interactionInProgress = React.useRef(inProgress !== msalBrowser.InteractionStatus.None);
450
- React.useEffect(() => {
451
- interactionInProgress.current = inProgress !== msalBrowser.InteractionStatus.None;
452
- }, [inProgress]); // Flag used to control when the hook calls login/acquireToken
453
-
454
- const shouldAcquireToken = React.useRef(true);
455
- React.useEffect(() => {
456
- if (!!error) {
457
- // Errors should be handled by consuming component
458
- shouldAcquireToken.current = false;
459
- return;
460
- }
461
-
462
- if (!!result) {
463
- // Token has already been acquired, consuming component/application is responsible for renewing
464
- shouldAcquireToken.current = false;
465
- return;
466
- }
467
- }, [error, result]);
468
- const login = React.useCallback(async (callbackInteractionType, callbackRequest) => {
469
- const loginType = callbackInteractionType || interactionType;
470
- const loginRequest = callbackRequest || authenticationRequest;
471
-
472
- switch (loginType) {
473
- case msalBrowser.InteractionType.Popup:
474
- logger.verbose("useMsalAuthentication - Calling loginPopup");
475
- return instance.loginPopup(loginRequest);
476
-
477
- case msalBrowser.InteractionType.Redirect:
478
- // This promise is not expected to resolve due to full frame redirect
479
- logger.verbose("useMsalAuthentication - Calling loginRedirect");
480
- return instance.loginRedirect(loginRequest).then(null);
481
-
482
- case msalBrowser.InteractionType.Silent:
483
- logger.verbose("useMsalAuthentication - Calling ssoSilent");
484
- return instance.ssoSilent(loginRequest);
485
-
486
- default:
487
- throw ReactAuthError.createInvalidInteractionTypeError();
488
- }
489
- }, [instance, interactionType, authenticationRequest, logger]);
490
- const acquireToken = React.useCallback(async (callbackInteractionType, callbackRequest) => {
491
- const fallbackInteractionType = callbackInteractionType || interactionType;
492
- let tokenRequest;
493
-
494
- if (callbackRequest) {
495
- logger.trace("useMsalAuthentication - acquireToken - Using request provided in the callback");
496
- tokenRequest = { ...callbackRequest
497
- };
498
- } else if (authenticationRequest) {
499
- logger.trace("useMsalAuthentication - acquireToken - Using request provided in the hook");
500
- tokenRequest = { ...authenticationRequest,
501
- scopes: authenticationRequest.scopes || msalBrowser.OIDC_DEFAULT_SCOPES
502
- };
503
- } else {
504
- logger.trace("useMsalAuthentication - acquireToken - No request object provided, using default request.");
505
- tokenRequest = {
506
- scopes: msalBrowser.OIDC_DEFAULT_SCOPES
507
- };
508
- }
509
-
510
- if (!tokenRequest.account && account) {
511
- logger.trace("useMsalAuthentication - acquireToken - Attaching account to request");
512
- tokenRequest.account = account;
513
- }
514
-
515
- const getToken = async () => {
516
- logger.verbose("useMsalAuthentication - Calling acquireTokenSilent");
517
- return instance.acquireTokenSilent(tokenRequest).catch(async e => {
518
- if (e instanceof msalBrowser.InteractionRequiredAuthError) {
519
- if (!interactionInProgress.current) {
520
- logger.error("useMsalAuthentication - Interaction required, falling back to interaction");
521
- return login(fallbackInteractionType, tokenRequest);
522
- } else {
523
- logger.error("useMsalAuthentication - Interaction required but is already in progress. Please try again, if needed, after interaction completes.");
524
- throw ReactAuthError.createUnableToFallbackToInteractionError();
525
- }
526
- }
527
-
528
- throw e;
529
- });
530
- };
531
-
532
- return getToken().then(response => {
533
- if (mounted.current) {
534
- setResponse([response, null]);
535
- }
536
-
537
- return response;
538
- }).catch(e => {
539
- if (mounted.current) {
540
- setResponse([null, e]);
541
- }
542
-
543
- throw e;
544
- });
545
- }, [instance, interactionType, authenticationRequest, logger, account, login]);
546
- React.useEffect(() => {
547
- const callbackId = instance.addEventCallback(message => {
548
- switch (message.eventType) {
549
- case msalBrowser.EventType.LOGIN_SUCCESS:
550
- case msalBrowser.EventType.SSO_SILENT_SUCCESS:
551
- if (message.payload) {
552
- setResponse([message.payload, null]);
553
- }
554
-
555
- break;
556
-
557
- case msalBrowser.EventType.LOGIN_FAILURE:
558
- case msalBrowser.EventType.SSO_SILENT_FAILURE:
559
- if (message.error) {
560
- setResponse([null, message.error]);
561
- }
562
-
563
- break;
564
- }
565
- });
566
- logger.verbose(`useMsalAuthentication - Registered event callback with id: ${callbackId}`);
567
- return () => {
568
- if (callbackId) {
569
- logger.verbose(`useMsalAuthentication - Removing event callback ${callbackId}`);
570
- instance.removeEventCallback(callbackId);
571
- }
572
- };
573
- }, [instance, logger]);
574
- React.useEffect(() => {
575
- if (shouldAcquireToken.current && inProgress === msalBrowser.InteractionStatus.None) {
576
- shouldAcquireToken.current = false;
577
-
578
- if (!isAuthenticated) {
579
- logger.info("useMsalAuthentication - No user is authenticated, attempting to login");
580
- login().catch(() => {
581
- // Errors are saved in state above
582
- return;
583
- });
584
- } else if (account) {
585
- logger.info("useMsalAuthentication - User is authenticated, attempting to acquire token");
586
- acquireToken().catch(() => {
587
- // Errors are saved in state above
588
- return;
589
- });
590
- }
591
- }
592
- }, [isAuthenticated, account, inProgress, login, acquireToken, logger]);
593
- return {
594
- login,
595
- acquireToken,
596
- result,
597
- error
598
- };
599
- }
600
-
601
- /*
602
- * Copyright (c) Microsoft Corporation. All rights reserved.
603
- * Licensed under the MIT License.
604
- */
605
- /**
606
- * Attempts to authenticate user if not already authenticated, then renders child components
607
- * @param props
608
- */
609
-
610
- function MsalAuthenticationTemplate(_ref) {
611
- let {
612
- interactionType,
613
- username,
614
- homeAccountId,
615
- localAccountId,
616
- authenticationRequest,
617
- loadingComponent: LoadingComponent,
618
- errorComponent: ErrorComponent,
619
- children
620
- } = _ref;
621
- const accountIdentifier = React.useMemo(() => {
622
- return {
623
- username,
624
- homeAccountId,
625
- localAccountId
626
- };
627
- }, [username, homeAccountId, localAccountId]);
628
- const context = useMsal();
629
- const msalAuthResult = useMsalAuthentication(interactionType, authenticationRequest, accountIdentifier);
630
- const isAuthenticated = useIsAuthenticated(accountIdentifier);
631
-
632
- if (msalAuthResult.error && context.inProgress === msalBrowser.InteractionStatus.None) {
633
- if (!!ErrorComponent) {
634
- return React__default.createElement(ErrorComponent, Object.assign({}, msalAuthResult));
635
- }
636
-
637
- throw msalAuthResult.error;
638
- }
639
-
640
- if (isAuthenticated) {
641
- return React__default.createElement(React__default.Fragment, null, getChildrenOrFunction(children, msalAuthResult));
642
- }
643
-
644
- if (!!LoadingComponent && context.inProgress !== msalBrowser.InteractionStatus.None) {
645
- return React__default.createElement(LoadingComponent, Object.assign({}, context));
646
- }
647
-
648
- return null;
649
- }
650
-
651
- /*
652
- * Copyright (c) Microsoft Corporation. All rights reserved.
653
- * Licensed under the MIT License.
654
- */
655
- /**
656
- * Higher order component wraps provided component with msal by injecting msal context values into the component's props
657
- * @param Component
658
- */
659
-
660
- const withMsal = Component => {
661
- const ComponentWithMsal = props => {
662
- const msal = useMsal();
663
- return React__default.createElement(Component, Object.assign({}, props, {
664
- msalContext: msal
665
- }));
666
- };
667
-
668
- const componentName = Component.displayName || Component.name || "Component";
669
- ComponentWithMsal.displayName = `withMsal(${componentName})`;
670
- return ComponentWithMsal;
671
- };
672
-
673
- exports.AuthenticatedTemplate = AuthenticatedTemplate;
674
- exports.MsalAuthenticationTemplate = MsalAuthenticationTemplate;
675
- exports.MsalConsumer = MsalConsumer;
676
- exports.MsalContext = MsalContext;
677
- exports.MsalProvider = MsalProvider;
678
- exports.UnauthenticatedTemplate = UnauthenticatedTemplate;
679
- exports.useAccount = useAccount;
680
- exports.useIsAuthenticated = useIsAuthenticated;
681
- exports.useMsal = useMsal;
682
- exports.useMsalAuthentication = useMsalAuthentication;
683
- exports.version = version;
684
- exports.withMsal = withMsal;
685
- //# sourceMappingURL=msal-react.cjs.development.js.map