Skip to content

Commit 0a96adf

Browse files
committed
overhaul of the information exposed by the context
This will make it possible to work out if you're logged out or the data hasn't been loaded yet
1 parent 971ea39 commit 0a96adf

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

example/src/App.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ import {
1010
} from "@aaronpowell/react-static-web-apps-auth";
1111

1212
const UserDisplay = () => {
13-
const userInfo = useUserInfo();
14-
if (userInfo.identityProvider) {
13+
const { clientPrincipal, loaded } = useUserInfo();
14+
15+
if (!loaded) {
16+
return <p>Checking user info...</p>;
17+
}
18+
19+
if (clientPrincipal) {
1520
return (
1621
<div>
1722
<p>
18-
{userInfo.identityProvider} {userInfo.userDetails} {userInfo.userId}{" "}
19-
{userInfo.userRoles}
23+
{clientPrincipal.identityProvider} {clientPrincipal.userDetails}{" "}
24+
{clientPrincipal.userId} {clientPrincipal.userRoles}
2025
</p>
2126
<p>
2227
<Logout />
2328
</p>
2429
<p>
25-
<UserPurge provider={userInfo.identityProvider} />
30+
<UserPurge provider={clientPrincipal.identityProvider} />
2631
</p>
2732
</div>
2833
);

react-static-web-apps-auth/src/UserInfoContext.tsx

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,58 @@
11
import React, { useContext, useEffect, useState } from "react";
22
import { AuthProviders } from "./types";
33

4-
export type UserInfo = {
5-
identityProvider: undefined | AuthProviders;
6-
userId: string | undefined;
7-
userDetails: string | undefined;
4+
export type ClientPrincipal = {
5+
identityProvider: AuthProviders;
6+
userId: string;
7+
userDetails: string;
88
userRoles: string[];
99
};
1010

11-
const defaultUserInfo = {
12-
identityProvider: undefined,
13-
userId: undefined,
14-
userDetails: undefined,
15-
userRoles: [],
11+
export type UserInfoContext = {
12+
loaded: boolean;
13+
clientPrincipal: ClientPrincipal | null;
1614
};
1715

18-
const UserInfoContext = React.createContext<UserInfo>(defaultUserInfo);
16+
const UserInfoContext = React.createContext<UserInfoContext>({
17+
loaded: false,
18+
clientPrincipal: null,
19+
});
1920

2021
const UserInfoContextProvider = ({ children }: { children: JSX.Element }) => {
21-
const [userInfo, setUserInfo] = useState<UserInfo>(defaultUserInfo);
22+
const [
23+
clientPrincipal,
24+
setClientPrincipal,
25+
] = useState<ClientPrincipal | null>(null);
26+
const [loaded, setLoaded] = useState(false);
2227

2328
useEffect(() => {
2429
const run = async () => {
25-
const res = await fetch("/.auth/me");
26-
const json: { clientPrincipal: UserInfo | null } = await res.json();
27-
if (json.clientPrincipal) {
28-
setUserInfo(json.clientPrincipal);
30+
try {
31+
const res = await fetch("/.auth/me");
32+
const json: {
33+
clientPrincipal: ClientPrincipal | null;
34+
} = await res.json();
35+
if (json.clientPrincipal) {
36+
setClientPrincipal(json.clientPrincipal);
37+
}
38+
} catch (e) {
39+
if (window.location.hostname === "localhost") {
40+
console.warn(
41+
"Can't access the auth endoint. For local development, please use the Static Web Apps CLI to emulate authentication: https://github.com/azure/static-web-apps-cli"
42+
);
43+
} else {
44+
console.error(`Failed to unpack JSON.`, e);
45+
}
2946
}
47+
48+
setLoaded(true);
3049
};
3150

3251
run();
3352
}, []);
3453

3554
return (
36-
<UserInfoContext.Provider value={userInfo}>
55+
<UserInfoContext.Provider value={{ loaded, clientPrincipal }}>
3756
{children}
3857
</UserInfoContext.Provider>
3958
);

react-static-web-apps-auth/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export { StaticWebAuthLogins, LoginProviderProps } from "./StaticWebAuthLogins";
22
export {
3-
UserInfo,
3+
ClientPrincipal as UserInfo,
44
UserInfoContextProvider,
55
useUserInfo,
66
} from "./UserInfoContext";

0 commit comments

Comments
 (0)