Skip to content

Commit 1df27eb

Browse files
authored
Merge pull request #4 from aaronpowell/expose-cookie
feat: added swa cookie to context
2 parents de4343c + 08e719c commit 1df27eb

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
# Changelog for `react-static-web-apps-auth`
22

3+
## [1.4.0] - unreleased
4+
5+
### Added
6+
7+
- Expose the SWA auth cookie, incase you need it in the browser
8+
39
## [1.3.0] - 2022-05-16
410

511
### Changed
612

7-
* Using React 18
8-
* Internally using npm workspaces for better dependency management
13+
- Using React 18
14+
- Internally using npm workspaces for better dependency management
915

1016
## [1.2.0] - 2021-07-12
1117

react-static-web-apps-auth/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aaronpowell/react-static-web-apps-auth",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "A library to help creating authenticated React apps on Azure Static Web Apps",
55
"main": "build/index.js",
66
"types": "build/index.d.ts",

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,33 @@ export type ClientPrincipal = {
1111
export type ClientPrincipalContext = {
1212
loaded: boolean;
1313
clientPrincipal: ClientPrincipal | null;
14+
swaCookie: string | null;
1415
};
1516

1617
const ClientPrincipalContext = React.createContext<ClientPrincipalContext>({
1718
loaded: false,
1819
clientPrincipal: null,
20+
swaCookie: null,
1921
});
2022

23+
const cookieParser = (cookies: string): Record<string, string> =>
24+
cookies.split("; ").reduce(
25+
(obj, cookie) => ({
26+
...obj,
27+
[cookie.split("=")[0]]: cookie.split("=").slice(1).join("="),
28+
}),
29+
{}
30+
);
31+
2132
const ClientPrincipalContextProvider = ({
2233
children,
2334
}: {
2435
children: JSX.Element;
2536
}) => {
26-
const [
27-
clientPrincipal,
28-
setClientPrincipal,
29-
] = useState<ClientPrincipal | null>(null);
37+
const [clientPrincipal, setClientPrincipal] =
38+
useState<ClientPrincipal | null>(null);
3039
const [loaded, setLoaded] = useState(false);
40+
const [swaCookie, setSwaCookie] = useState<string | null>(null);
3141

3242
useEffect(() => {
3343
const run = async () => {
@@ -50,13 +60,18 @@ const ClientPrincipalContextProvider = ({
5060
}
5161

5262
setLoaded(true);
63+
setSwaCookie(
64+
cookieParser(document.cookie)["StaticWebAppsAuthCookie"] || null
65+
);
5366
};
5467

5568
run();
5669
}, []);
5770

5871
return (
59-
<ClientPrincipalContext.Provider value={{ loaded, clientPrincipal }}>
72+
<ClientPrincipalContext.Provider
73+
value={{ loaded, clientPrincipal, swaCookie }}
74+
>
6075
{children}
6176
</ClientPrincipalContext.Provider>
6277
);

0 commit comments

Comments
 (0)