Skip to content

Commit 775a3be

Browse files
authored
small component ui fix (#1414)
<!-- Make sure you've read the CONTRIBUTING.md guidelines: https://github.com/stack-auth/stack-auth/blob/dev/CONTRIBUTING.md --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Enhanced CLI authentication confirmation tracking to improve session persistence and state management during sign-in flows. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 24245ae commit 775a3be

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

packages/template/src/components-page/cli-auth-confirm.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe("useCliAuthConfirmation", () => {
171171
accessToken: "access-token",
172172
refreshToken: "refresh-token",
173173
});
174-
expect(new URL(window.location.href).searchParams.get("confirmed")).toBe("true");
174+
expect(sessionStorage.getItem("stack-cli-auth-confirmed")).toBe("login-code");
175175
expect(sendRequest.mock.calls.map(call => JSON.parse(String(call[1].body)))).toMatchInlineSnapshot(`
176176
[
177177
{

packages/template/src/components-page/cli-auth-confirm.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,18 @@ async function completeCliAuthWithRefreshToken(app: StackClientApp, loginCode: s
2828
await ensureCliCompleteOk(result);
2929
}
3030

31-
function markUrlConfirmed() {
32-
const url = new URL(window.location.href);
33-
url.searchParams.set("confirmed", "true");
34-
window.history.replaceState({}, "", url.toString());
31+
const CLI_AUTH_CONFIRMED_KEY = "stack-cli-auth-confirmed";
32+
33+
function markConfirmed(loginCode: string) {
34+
sessionStorage.setItem(CLI_AUTH_CONFIRMED_KEY, loginCode);
35+
}
36+
37+
function isConfirmed(loginCode: string): boolean {
38+
return sessionStorage.getItem(CLI_AUTH_CONFIRMED_KEY) === loginCode;
39+
}
40+
41+
function clearConfirmed() {
42+
sessionStorage.removeItem(CLI_AUTH_CONFIRMED_KEY);
3543
}
3644

3745
function getError(err: unknown): Error {
@@ -79,7 +87,7 @@ export function useCliAuthConfirmation(): CliAuthConfirmationState {
7987
});
8088
const [confirmed] = useState(() => {
8189
if (typeof window === 'undefined') return false;
82-
return new URLSearchParams(window.location.search).get("confirmed") === "true";
90+
return loginCode != null && isConfirmed(loginCode);
8391
});
8492

8593
const completeWithCurrentUser = useCallback(async () => {
@@ -105,6 +113,7 @@ export function useCliAuthConfirmation(): CliAuthConfirmationState {
105113
setStatus("authorizing");
106114
try {
107115
await completeWithCurrentUser();
116+
clearConfirmed();
108117
setStatus("success");
109118
} catch (err) {
110119
setError(getError(err));
@@ -130,6 +139,7 @@ export function useCliAuthConfirmation(): CliAuthConfirmationState {
130139
setStatus("authorizing");
131140
if (user) {
132141
await completeWithCurrentUser();
142+
clearConfirmed();
133143
setStatus("success");
134144
return;
135145
}
@@ -158,17 +168,13 @@ export function useCliAuthConfirmation(): CliAuthConfirmationState {
158168
accessToken,
159169
refreshToken,
160170
});
161-
// Only mark the URL as confirmed once the anon session is actually
162-
// bound to the browser; otherwise a failure above would leave a stale
163-
// confirmed=true in the URL and the auto-complete effect would later
164-
// bind the CLI to whichever user happens to be signed in.
165-
markUrlConfirmed();
171+
markConfirmed(loginCode);
166172
setStatus("redirecting");
167173
await app.redirectToSignUp({ replace: true });
168174
return;
169175
}
170176

171-
markUrlConfirmed();
177+
markConfirmed(loginCode);
172178
setStatus("redirecting");
173179
await app.redirectToSignIn({ replace: true });
174180
} catch (err) {

0 commit comments

Comments
 (0)