Skip to content

Commit b52dd40

Browse files
committed
[solid-react] Update session and ranInitialCheck synchronously
during initial auth check. Fixes #52
1 parent 7d5b421 commit b52dd40

2 files changed

Lines changed: 79 additions & 13 deletions

File tree

packages/solid-react/src/createBrowserSolidReactMethods.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ export function createBrowserSolidReactMethods(
3939
window.localStorage.setItem(PRE_REDIRECT_URI, window.location.href);
4040
}
4141

42-
await handleIncomingRedirect({
42+
const sessionInfo = await handleIncomingRedirect({
4343
restorePreviousSession: true,
4444
});
45-
// Set timout to ensure this happens after the redirect
46-
setTimeout(() => {
47-
setSession({ ...getDefaultSession().info });
48-
window.history.replaceState(
49-
{},
50-
"",
51-
window.localStorage.getItem(PRE_REDIRECT_URI),
52-
);
53-
window.localStorage.removeItem(PRE_REDIRECT_URI);
5445

55-
setRanInitialAuthCheck(true);
56-
}, 0);
46+
if (!sessionInfo) return;
47+
48+
setSession(sessionInfo);
49+
window.history.replaceState(
50+
{},
51+
"",
52+
window.localStorage.getItem(PRE_REDIRECT_URI),
53+
);
54+
window.localStorage.removeItem(PRE_REDIRECT_URI);
55+
56+
setRanInitialAuthCheck(true);
5757
}, []);
5858

5959
const login = useCallback(
@@ -69,7 +69,7 @@ export function createBrowserSolidReactMethods(
6969
};
7070
window.localStorage.setItem(PRE_REDIRECT_URI, window.location.href);
7171
await libraryLogin(fullOptions);
72-
setSession({ ...getDefaultSession().info });
72+
// user should be redirected away from the app
7373
},
7474
[],
7575
);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React, { type FunctionComponent, useEffect } from "react";
2+
import { beforeEach, describe, it, vi, expect } from "vitest";
3+
import { useSolidAuth } from "../src/SolidAuthContext.js";
4+
import { render, waitFor } from "@testing-library/react";
5+
import { BrowserSolidLdoProvider } from "@ldo/solid-react";
6+
import { handleIncomingRedirect } from "@inrupt/solid-client-authn-browser";
7+
8+
window.localStorage = {
9+
length: 0,
10+
getItem: () => null,
11+
setItem: () => {},
12+
clear: () => {},
13+
removeItem: () => {},
14+
key: () => null,
15+
};
16+
17+
vi.mock("@inrupt/solid-client-authn-browser", async (importOriginal) => {
18+
const actual = await importOriginal();
19+
return {
20+
...(actual as Record<string, unknown>),
21+
handleIncomingRedirect: vi.fn(),
22+
};
23+
});
24+
25+
describe("Solid Auth Integration Tests", () => {
26+
beforeEach(() => {
27+
vi.clearAllMocks();
28+
});
29+
30+
// https://github.com/o-development/ldo/issues/52
31+
it("keeps ranInitialAuthCheck and session.isLoggedIn in sync", async () => {
32+
const renders: ReturnType<typeof useSolidAuth>[] = [];
33+
34+
vi.mocked(handleIncomingRedirect).mockResolvedValue({
35+
isLoggedIn: true,
36+
sessionId: "handle-incoming-redirect",
37+
});
38+
39+
const TestComponent: FunctionComponent = () => {
40+
const result = useSolidAuth();
41+
useEffect(() => {
42+
renders.push(result);
43+
});
44+
return null;
45+
};
46+
47+
render(
48+
<BrowserSolidLdoProvider>
49+
<TestComponent />
50+
</BrowserSolidLdoProvider>,
51+
);
52+
53+
await waitFor(() => {
54+
expect(renders.some((render) => render.session.isLoggedIn)).toBe(true);
55+
expect(renders.some((render) => render.ranInitialAuthCheck)).toBe(true);
56+
});
57+
58+
for (const renderData of renders) {
59+
if (renderData.ranInitialAuthCheck === true) {
60+
expect(renderData.session.isLoggedIn).toEqual(true);
61+
} else {
62+
expect(renderData.session.isLoggedIn).toEqual(false);
63+
}
64+
}
65+
});
66+
});

0 commit comments

Comments
 (0)