@azure/msal-react 2.0.1 → 2.0.3
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 +10 -0
- package/dist/MsalContext.js +1 -1
- package/dist/MsalContext.js.map +1 -1
- package/dist/MsalProvider.js +8 -2
- package/dist/MsalProvider.js.map +1 -1
- package/dist/components/AuthenticatedTemplate.js +1 -1
- package/dist/components/AuthenticatedTemplate.js.map +1 -1
- package/dist/components/MsalAuthenticationTemplate.js +1 -1
- package/dist/components/MsalAuthenticationTemplate.js.map +1 -1
- package/dist/components/UnauthenticatedTemplate.js +1 -1
- package/dist/components/UnauthenticatedTemplate.js.map +1 -1
- package/dist/components/withMsal.js +1 -1
- package/dist/components/withMsal.js.map +1 -1
- package/dist/error/ReactAuthError.js +1 -1
- package/dist/error/ReactAuthError.js.map +1 -1
- package/dist/hooks/useAccount.js +1 -1
- package/dist/hooks/useAccount.js.map +1 -1
- package/dist/hooks/useIsAuthenticated.js +1 -1
- package/dist/hooks/useIsAuthenticated.js.map +1 -1
- package/dist/hooks/useMsal.js +1 -1
- package/dist/hooks/useMsal.js.map +1 -1
- package/dist/hooks/useMsalAuthentication.js +1 -1
- package/dist/hooks/useMsalAuthentication.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/packageMetadata.d.ts +1 -1
- package/dist/packageMetadata.js +2 -2
- package/dist/packageMetadata.js.map +1 -1
- package/dist/utils/utilities.d.ts +2 -2
- package/dist/utils/utilities.js +1 -1
- package/dist/utils/utilities.js.map +1 -1
- package/package.json +12 -7
- package/src/MsalContext.ts +35 -0
- package/src/MsalProvider.tsx +210 -0
- package/src/components/AuthenticatedTemplate.tsx +43 -0
- package/src/components/MsalAuthenticationTemplate.tsx +86 -0
- package/src/components/UnauthenticatedTemplate.tsx +48 -0
- package/src/components/withMsal.tsx +34 -0
- package/src/error/ReactAuthError.ts +40 -0
- package/src/hooks/useAccount.ts +68 -0
- package/src/hooks/useIsAuthenticated.ts +47 -0
- package/src/hooks/useMsal.ts +12 -0
- package/src/hooks/useMsalAuthentication.ts +283 -0
- package/src/index.ts +35 -0
- package/src/packageMetadata.ts +3 -0
- package/src/types/AccountIdentifiers.ts +10 -0
- package/src/utils/utilities.ts +102 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import React, {
|
|
7
|
+
useEffect,
|
|
8
|
+
useReducer,
|
|
9
|
+
PropsWithChildren,
|
|
10
|
+
useMemo,
|
|
11
|
+
} from "react";
|
|
12
|
+
import {
|
|
13
|
+
IPublicClientApplication,
|
|
14
|
+
EventMessage,
|
|
15
|
+
EventMessageUtils,
|
|
16
|
+
InteractionStatus,
|
|
17
|
+
Logger,
|
|
18
|
+
WrapperSKU,
|
|
19
|
+
AccountInfo,
|
|
20
|
+
} from "@azure/msal-browser";
|
|
21
|
+
import { MsalContext, IMsalContext } from "./MsalContext";
|
|
22
|
+
import { accountArraysAreEqual } from "./utils/utilities";
|
|
23
|
+
import { name as SKU, version } from "./packageMetadata";
|
|
24
|
+
|
|
25
|
+
export type MsalProviderProps = PropsWithChildren<{
|
|
26
|
+
instance: IPublicClientApplication;
|
|
27
|
+
}>;
|
|
28
|
+
|
|
29
|
+
type MsalState = {
|
|
30
|
+
inProgress: InteractionStatus;
|
|
31
|
+
accounts: AccountInfo[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const MsalProviderActionType = {
|
|
35
|
+
UNBLOCK_INPROGRESS: "UNBLOCK_INPROGRESS",
|
|
36
|
+
EVENT: "EVENT",
|
|
37
|
+
} as const;
|
|
38
|
+
type MsalProviderActionType =
|
|
39
|
+
(typeof MsalProviderActionType)[keyof typeof MsalProviderActionType];
|
|
40
|
+
|
|
41
|
+
type MsalProviderReducerAction = {
|
|
42
|
+
type: MsalProviderActionType;
|
|
43
|
+
payload: {
|
|
44
|
+
logger: Logger;
|
|
45
|
+
instance: IPublicClientApplication;
|
|
46
|
+
message?: EventMessage;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Returns the next inProgress and accounts state based on event message
|
|
52
|
+
* @param previousState
|
|
53
|
+
* @param action
|
|
54
|
+
*/
|
|
55
|
+
const reducer = (
|
|
56
|
+
previousState: MsalState,
|
|
57
|
+
action: MsalProviderReducerAction
|
|
58
|
+
): MsalState => {
|
|
59
|
+
const { type, payload } = action;
|
|
60
|
+
let newInProgress = previousState.inProgress;
|
|
61
|
+
|
|
62
|
+
switch (type) {
|
|
63
|
+
case MsalProviderActionType.UNBLOCK_INPROGRESS:
|
|
64
|
+
if (previousState.inProgress === InteractionStatus.Startup) {
|
|
65
|
+
newInProgress = InteractionStatus.None;
|
|
66
|
+
payload.logger.info(
|
|
67
|
+
"MsalProvider - handleRedirectPromise resolved, setting inProgress to 'none'"
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
case MsalProviderActionType.EVENT:
|
|
72
|
+
const message = payload.message as EventMessage;
|
|
73
|
+
const status = EventMessageUtils.getInteractionStatusFromEvent(
|
|
74
|
+
message,
|
|
75
|
+
previousState.inProgress
|
|
76
|
+
);
|
|
77
|
+
if (status) {
|
|
78
|
+
payload.logger.info(
|
|
79
|
+
`MsalProvider - ${message.eventType} results in setting inProgress from ${previousState.inProgress} to ${status}`
|
|
80
|
+
);
|
|
81
|
+
newInProgress = status;
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
default:
|
|
85
|
+
throw new Error(`Unknown action type: ${type}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const currentAccounts = payload.instance.getAllAccounts();
|
|
89
|
+
if (
|
|
90
|
+
newInProgress !== previousState.inProgress &&
|
|
91
|
+
!accountArraysAreEqual(currentAccounts, previousState.accounts)
|
|
92
|
+
) {
|
|
93
|
+
// Both inProgress and accounts changed
|
|
94
|
+
return {
|
|
95
|
+
...previousState,
|
|
96
|
+
inProgress: newInProgress,
|
|
97
|
+
accounts: currentAccounts,
|
|
98
|
+
};
|
|
99
|
+
} else if (newInProgress !== previousState.inProgress) {
|
|
100
|
+
// Only only inProgress changed
|
|
101
|
+
return {
|
|
102
|
+
...previousState,
|
|
103
|
+
inProgress: newInProgress,
|
|
104
|
+
};
|
|
105
|
+
} else if (
|
|
106
|
+
!accountArraysAreEqual(currentAccounts, previousState.accounts)
|
|
107
|
+
) {
|
|
108
|
+
// Only accounts changed
|
|
109
|
+
return {
|
|
110
|
+
...previousState,
|
|
111
|
+
accounts: currentAccounts,
|
|
112
|
+
};
|
|
113
|
+
} else {
|
|
114
|
+
// Nothing changed
|
|
115
|
+
return previousState;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* MSAL context provider component. This must be rendered above any other components that use MSAL.
|
|
121
|
+
*/
|
|
122
|
+
export function MsalProvider({
|
|
123
|
+
instance,
|
|
124
|
+
children,
|
|
125
|
+
}: MsalProviderProps): React.ReactElement {
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
instance.initializeWrapperLibrary(WrapperSKU.React, version);
|
|
128
|
+
}, [instance]);
|
|
129
|
+
// Create a logger instance for msal-react with the same options as PublicClientApplication
|
|
130
|
+
const logger = useMemo(() => {
|
|
131
|
+
return instance.getLogger().clone(SKU, version);
|
|
132
|
+
}, [instance]);
|
|
133
|
+
|
|
134
|
+
const [state, updateState] = useReducer(reducer, undefined, () => {
|
|
135
|
+
// Lazy initialization of the initial state
|
|
136
|
+
return {
|
|
137
|
+
inProgress: InteractionStatus.Startup,
|
|
138
|
+
accounts: instance.getAllAccounts(),
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
useEffect(() => {
|
|
143
|
+
const callbackId = instance.addEventCallback(
|
|
144
|
+
(message: EventMessage) => {
|
|
145
|
+
updateState({
|
|
146
|
+
payload: {
|
|
147
|
+
instance,
|
|
148
|
+
logger,
|
|
149
|
+
message,
|
|
150
|
+
},
|
|
151
|
+
type: MsalProviderActionType.EVENT,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
);
|
|
155
|
+
logger.verbose(
|
|
156
|
+
`MsalProvider - Registered event callback with id: ${callbackId}`
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
instance
|
|
160
|
+
.initialize()
|
|
161
|
+
.then(() => {
|
|
162
|
+
instance
|
|
163
|
+
.handleRedirectPromise()
|
|
164
|
+
.catch(() => {
|
|
165
|
+
// Errors should be handled by listening to the LOGIN_FAILURE event
|
|
166
|
+
return;
|
|
167
|
+
})
|
|
168
|
+
.finally(() => {
|
|
169
|
+
/*
|
|
170
|
+
* If handleRedirectPromise returns a cached promise the necessary events may not be fired
|
|
171
|
+
* This is a fallback to prevent inProgress from getting stuck in 'startup'
|
|
172
|
+
*/
|
|
173
|
+
updateState({
|
|
174
|
+
payload: {
|
|
175
|
+
instance,
|
|
176
|
+
logger,
|
|
177
|
+
},
|
|
178
|
+
type: MsalProviderActionType.UNBLOCK_INPROGRESS,
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
})
|
|
182
|
+
.catch(() => {
|
|
183
|
+
// Errors should be handled by listening to the LOGIN_FAILURE event
|
|
184
|
+
return;
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
return () => {
|
|
188
|
+
// Remove callback when component unmounts or accounts change
|
|
189
|
+
if (callbackId) {
|
|
190
|
+
logger.verbose(
|
|
191
|
+
`MsalProvider - Removing event callback ${callbackId}`
|
|
192
|
+
);
|
|
193
|
+
instance.removeEventCallback(callbackId);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}, [instance, logger]);
|
|
197
|
+
|
|
198
|
+
const contextValue: IMsalContext = {
|
|
199
|
+
instance,
|
|
200
|
+
inProgress: state.inProgress,
|
|
201
|
+
accounts: state.accounts,
|
|
202
|
+
logger,
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
return (
|
|
206
|
+
<MsalContext.Provider value={contextValue}>
|
|
207
|
+
{children}
|
|
208
|
+
</MsalContext.Provider>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import React, { PropsWithChildren, useMemo } from "react";
|
|
7
|
+
import { AccountIdentifiers } from "../types/AccountIdentifiers";
|
|
8
|
+
import { getChildrenOrFunction } from "../utils/utilities";
|
|
9
|
+
import { useMsal } from "../hooks/useMsal";
|
|
10
|
+
import { useIsAuthenticated } from "../hooks/useIsAuthenticated";
|
|
11
|
+
import { InteractionStatus } from "@azure/msal-browser";
|
|
12
|
+
|
|
13
|
+
export type AuthenticatedTemplateProps = PropsWithChildren<AccountIdentifiers>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Renders child components if user is authenticated
|
|
17
|
+
* @param props
|
|
18
|
+
*/
|
|
19
|
+
export function AuthenticatedTemplate({
|
|
20
|
+
username,
|
|
21
|
+
homeAccountId,
|
|
22
|
+
localAccountId,
|
|
23
|
+
children,
|
|
24
|
+
}: AuthenticatedTemplateProps): React.ReactElement | null {
|
|
25
|
+
const context = useMsal();
|
|
26
|
+
const accountIdentifier: AccountIdentifiers = useMemo(() => {
|
|
27
|
+
return {
|
|
28
|
+
username,
|
|
29
|
+
homeAccountId,
|
|
30
|
+
localAccountId,
|
|
31
|
+
};
|
|
32
|
+
}, [username, homeAccountId, localAccountId]);
|
|
33
|
+
const isAuthenticated = useIsAuthenticated(accountIdentifier);
|
|
34
|
+
|
|
35
|
+
if (isAuthenticated && context.inProgress !== InteractionStatus.Startup) {
|
|
36
|
+
return (
|
|
37
|
+
<React.Fragment>
|
|
38
|
+
{getChildrenOrFunction(children, context)}
|
|
39
|
+
</React.Fragment>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import React, { PropsWithChildren, useMemo } from "react";
|
|
7
|
+
import { AccountIdentifiers } from "../types/AccountIdentifiers";
|
|
8
|
+
import { getChildrenOrFunction } from "../utils/utilities";
|
|
9
|
+
import { useMsal } from "../hooks/useMsal";
|
|
10
|
+
import {
|
|
11
|
+
MsalAuthenticationResult,
|
|
12
|
+
useMsalAuthentication,
|
|
13
|
+
} from "../hooks/useMsalAuthentication";
|
|
14
|
+
import { useIsAuthenticated } from "../hooks/useIsAuthenticated";
|
|
15
|
+
import {
|
|
16
|
+
InteractionType,
|
|
17
|
+
PopupRequest,
|
|
18
|
+
RedirectRequest,
|
|
19
|
+
SsoSilentRequest,
|
|
20
|
+
InteractionStatus,
|
|
21
|
+
} from "@azure/msal-browser";
|
|
22
|
+
import { IMsalContext } from "../MsalContext";
|
|
23
|
+
|
|
24
|
+
export type MsalAuthenticationProps = PropsWithChildren<
|
|
25
|
+
AccountIdentifiers & {
|
|
26
|
+
interactionType: InteractionType;
|
|
27
|
+
authenticationRequest?:
|
|
28
|
+
| PopupRequest
|
|
29
|
+
| RedirectRequest
|
|
30
|
+
| SsoSilentRequest;
|
|
31
|
+
loadingComponent?: React.ElementType<IMsalContext>;
|
|
32
|
+
errorComponent?: React.ElementType<MsalAuthenticationResult>;
|
|
33
|
+
}
|
|
34
|
+
>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Attempts to authenticate user if not already authenticated, then renders child components
|
|
38
|
+
* @param props
|
|
39
|
+
*/
|
|
40
|
+
export function MsalAuthenticationTemplate({
|
|
41
|
+
interactionType,
|
|
42
|
+
username,
|
|
43
|
+
homeAccountId,
|
|
44
|
+
localAccountId,
|
|
45
|
+
authenticationRequest,
|
|
46
|
+
loadingComponent: LoadingComponent,
|
|
47
|
+
errorComponent: ErrorComponent,
|
|
48
|
+
children,
|
|
49
|
+
}: MsalAuthenticationProps): React.ReactElement | null {
|
|
50
|
+
const accountIdentifier: AccountIdentifiers = useMemo(() => {
|
|
51
|
+
return {
|
|
52
|
+
username,
|
|
53
|
+
homeAccountId,
|
|
54
|
+
localAccountId,
|
|
55
|
+
};
|
|
56
|
+
}, [username, homeAccountId, localAccountId]);
|
|
57
|
+
const context = useMsal();
|
|
58
|
+
const msalAuthResult = useMsalAuthentication(
|
|
59
|
+
interactionType,
|
|
60
|
+
authenticationRequest,
|
|
61
|
+
accountIdentifier
|
|
62
|
+
);
|
|
63
|
+
const isAuthenticated = useIsAuthenticated(accountIdentifier);
|
|
64
|
+
|
|
65
|
+
if (msalAuthResult.error && context.inProgress === InteractionStatus.None) {
|
|
66
|
+
if (!!ErrorComponent) {
|
|
67
|
+
return <ErrorComponent {...msalAuthResult} />;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
throw msalAuthResult.error;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (isAuthenticated) {
|
|
74
|
+
return (
|
|
75
|
+
<React.Fragment>
|
|
76
|
+
{getChildrenOrFunction(children, msalAuthResult)}
|
|
77
|
+
</React.Fragment>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!!LoadingComponent && context.inProgress !== InteractionStatus.None) {
|
|
82
|
+
return <LoadingComponent {...context} />;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import React, { PropsWithChildren, useMemo } from "react";
|
|
7
|
+
import { useMsal } from "../hooks/useMsal";
|
|
8
|
+
import { useIsAuthenticated } from "../hooks/useIsAuthenticated";
|
|
9
|
+
import { getChildrenOrFunction } from "../utils/utilities";
|
|
10
|
+
import { AccountIdentifiers } from "../types/AccountIdentifiers";
|
|
11
|
+
import { InteractionStatus } from "@azure/msal-browser";
|
|
12
|
+
|
|
13
|
+
export type UnauthenticatedTemplateProps =
|
|
14
|
+
PropsWithChildren<AccountIdentifiers>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Renders child components if user is unauthenticated
|
|
18
|
+
* @param props
|
|
19
|
+
*/
|
|
20
|
+
export function UnauthenticatedTemplate({
|
|
21
|
+
username,
|
|
22
|
+
homeAccountId,
|
|
23
|
+
localAccountId,
|
|
24
|
+
children,
|
|
25
|
+
}: UnauthenticatedTemplateProps): React.ReactElement | null {
|
|
26
|
+
const context = useMsal();
|
|
27
|
+
const accountIdentifier: AccountIdentifiers = useMemo(() => {
|
|
28
|
+
return {
|
|
29
|
+
username,
|
|
30
|
+
homeAccountId,
|
|
31
|
+
localAccountId,
|
|
32
|
+
};
|
|
33
|
+
}, [username, homeAccountId, localAccountId]);
|
|
34
|
+
const isAuthenticated = useIsAuthenticated(accountIdentifier);
|
|
35
|
+
|
|
36
|
+
if (
|
|
37
|
+
!isAuthenticated &&
|
|
38
|
+
context.inProgress !== InteractionStatus.Startup &&
|
|
39
|
+
context.inProgress !== InteractionStatus.HandleRedirect
|
|
40
|
+
) {
|
|
41
|
+
return (
|
|
42
|
+
<React.Fragment>
|
|
43
|
+
{getChildrenOrFunction(children, context)}
|
|
44
|
+
</React.Fragment>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import React from "react";
|
|
7
|
+
import { IMsalContext } from "../MsalContext";
|
|
8
|
+
import { useMsal } from "../hooks/useMsal";
|
|
9
|
+
import { Subtract } from "../utils/utilities";
|
|
10
|
+
|
|
11
|
+
export type WithMsalProps = {
|
|
12
|
+
msalContext: IMsalContext;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Higher order component wraps provided component with msal by injecting msal context values into the component's props
|
|
17
|
+
* @param Component
|
|
18
|
+
*/
|
|
19
|
+
export const withMsal = <P extends WithMsalProps>(
|
|
20
|
+
Component: React.ComponentType<P>
|
|
21
|
+
): React.FunctionComponent<Subtract<P, WithMsalProps>> => {
|
|
22
|
+
const ComponentWithMsal: React.FunctionComponent<
|
|
23
|
+
Subtract<P, WithMsalProps>
|
|
24
|
+
> = (props) => {
|
|
25
|
+
const msal = useMsal();
|
|
26
|
+
return <Component {...(props as P)} msalContext={msal} />;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const componentName =
|
|
30
|
+
Component.displayName || Component.name || "Component";
|
|
31
|
+
ComponentWithMsal.displayName = `withMsal(${componentName})`;
|
|
32
|
+
|
|
33
|
+
return ComponentWithMsal;
|
|
34
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { AuthError } from "@azure/msal-browser";
|
|
7
|
+
|
|
8
|
+
export const ReactAuthErrorMessage = {
|
|
9
|
+
invalidInteractionType: {
|
|
10
|
+
code: "invalid_interaction_type",
|
|
11
|
+
desc: "The provided interaction type is invalid.",
|
|
12
|
+
},
|
|
13
|
+
unableToFallbackToInteraction: {
|
|
14
|
+
code: "unable_to_fallback_to_interaction",
|
|
15
|
+
desc: "Interaction is required but another interaction is already in progress. Please try again when the current interaction is complete.",
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export class ReactAuthError extends AuthError {
|
|
20
|
+
constructor(errorCode: string, errorMessage?: string) {
|
|
21
|
+
super(errorCode, errorMessage);
|
|
22
|
+
|
|
23
|
+
Object.setPrototypeOf(this, ReactAuthError.prototype);
|
|
24
|
+
this.name = "ReactAuthError";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static createInvalidInteractionTypeError(): ReactAuthError {
|
|
28
|
+
return new ReactAuthError(
|
|
29
|
+
ReactAuthErrorMessage.invalidInteractionType.code,
|
|
30
|
+
ReactAuthErrorMessage.invalidInteractionType.desc
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static createUnableToFallbackToInteractionError(): ReactAuthError {
|
|
35
|
+
return new ReactAuthError(
|
|
36
|
+
ReactAuthErrorMessage.unableToFallbackToInteraction.code,
|
|
37
|
+
ReactAuthErrorMessage.unableToFallbackToInteraction.desc
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { useState, useEffect } from "react";
|
|
7
|
+
import {
|
|
8
|
+
AccountInfo,
|
|
9
|
+
IPublicClientApplication,
|
|
10
|
+
AccountEntity,
|
|
11
|
+
} from "@azure/msal-browser";
|
|
12
|
+
import { useMsal } from "./useMsal";
|
|
13
|
+
import { AccountIdentifiers } from "../types/AccountIdentifiers";
|
|
14
|
+
import { getAccountByIdentifiers } from "../utils/utilities";
|
|
15
|
+
|
|
16
|
+
function getAccount(
|
|
17
|
+
instance: IPublicClientApplication,
|
|
18
|
+
accountIdentifiers?: AccountIdentifiers
|
|
19
|
+
): AccountInfo | null {
|
|
20
|
+
if (
|
|
21
|
+
!accountIdentifiers ||
|
|
22
|
+
(!accountIdentifiers.homeAccountId &&
|
|
23
|
+
!accountIdentifiers.localAccountId &&
|
|
24
|
+
!accountIdentifiers.username)
|
|
25
|
+
) {
|
|
26
|
+
// If no account identifiers are provided, return active account
|
|
27
|
+
return instance.getActiveAccount();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return getAccountByIdentifiers(
|
|
31
|
+
instance.getAllAccounts(),
|
|
32
|
+
accountIdentifiers
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Given 1 or more accountIdentifiers, returns the Account object if the user is signed-in
|
|
38
|
+
* @param accountIdentifiers
|
|
39
|
+
*/
|
|
40
|
+
export function useAccount(
|
|
41
|
+
accountIdentifiers?: AccountIdentifiers
|
|
42
|
+
): AccountInfo | null {
|
|
43
|
+
const { instance, inProgress, logger } = useMsal();
|
|
44
|
+
|
|
45
|
+
const [account, setAccount] = useState<AccountInfo | null>(() =>
|
|
46
|
+
getAccount(instance, accountIdentifiers)
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
setAccount((currentAccount: AccountInfo | null) => {
|
|
51
|
+
const nextAccount = getAccount(instance, accountIdentifiers);
|
|
52
|
+
if (
|
|
53
|
+
!AccountEntity.accountInfoIsEqual(
|
|
54
|
+
currentAccount,
|
|
55
|
+
nextAccount,
|
|
56
|
+
true
|
|
57
|
+
)
|
|
58
|
+
) {
|
|
59
|
+
logger.info("useAccount - Updating account");
|
|
60
|
+
return nextAccount;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return currentAccount;
|
|
64
|
+
});
|
|
65
|
+
}, [inProgress, accountIdentifiers, instance, logger]);
|
|
66
|
+
|
|
67
|
+
return account;
|
|
68
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { useState, useEffect } from "react";
|
|
7
|
+
import { useMsal } from "./useMsal";
|
|
8
|
+
import { AccountIdentifiers } from "../types/AccountIdentifiers";
|
|
9
|
+
import { AccountInfo, InteractionStatus } from "@azure/msal-browser";
|
|
10
|
+
import { getAccountByIdentifiers } from "../utils/utilities";
|
|
11
|
+
|
|
12
|
+
function isAuthenticated(
|
|
13
|
+
allAccounts: AccountInfo[],
|
|
14
|
+
matchAccount?: AccountIdentifiers
|
|
15
|
+
): boolean {
|
|
16
|
+
if (
|
|
17
|
+
matchAccount &&
|
|
18
|
+
(matchAccount.username ||
|
|
19
|
+
matchAccount.homeAccountId ||
|
|
20
|
+
matchAccount.localAccountId)
|
|
21
|
+
) {
|
|
22
|
+
return !!getAccountByIdentifiers(allAccounts, matchAccount);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return allAccounts.length > 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 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
|
|
30
|
+
* @param matchAccount
|
|
31
|
+
*/
|
|
32
|
+
export function useIsAuthenticated(matchAccount?: AccountIdentifiers): boolean {
|
|
33
|
+
const { accounts: allAccounts, inProgress } = useMsal();
|
|
34
|
+
|
|
35
|
+
const [hasAuthenticated, setHasAuthenticated] = useState<boolean>(() => {
|
|
36
|
+
if (inProgress === InteractionStatus.Startup) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return isAuthenticated(allAccounts, matchAccount);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
setHasAuthenticated(isAuthenticated(allAccounts, matchAccount));
|
|
44
|
+
}, [allAccounts, matchAccount]);
|
|
45
|
+
|
|
46
|
+
return hasAuthenticated;
|
|
47
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { useContext } from "react";
|
|
7
|
+
import { IMsalContext, MsalContext } from "../MsalContext";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Returns Msal Context values
|
|
11
|
+
*/
|
|
12
|
+
export const useMsal = (): IMsalContext => useContext(MsalContext);
|