Skip to content

Commit 0dbf75a

Browse files
committed
store state before redirect
1 parent 82858be commit 0dbf75a

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

client/src/App.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,21 +234,36 @@ const App = () => {
234234
({
235235
authorizationCode,
236236
errorMsg,
237+
restoredState,
237238
}: {
238239
authorizationCode?: string;
239240
errorMsg?: string;
241+
restoredState?: AuthDebuggerState;
240242
}) => {
241243
setIsAuthDebuggerVisible(true);
242-
if (authorizationCode) {
244+
245+
if (restoredState) {
246+
// Restore the previous auth state
243247
updateAuthState({
244-
authorizationCode,
245-
oauthStep: "token_request",
246-
});
247-
}
248-
if (errorMsg) {
249-
updateAuthState({
250-
latestError: new Error(errorMsg),
248+
...restoredState,
249+
// Update with the new authorization code if provided
250+
authorizationCode: authorizationCode || restoredState.authorizationCode,
251+
oauthStep: authorizationCode ? "token_request" : restoredState.oauthStep,
252+
latestError: errorMsg ? new Error(errorMsg) : restoredState.latestError,
251253
});
254+
} else {
255+
// Fallback to the original behavior if no state was restored
256+
if (authorizationCode) {
257+
updateAuthState({
258+
authorizationCode,
259+
oauthStep: "token_request",
260+
});
261+
}
262+
if (errorMsg) {
263+
updateAuthState({
264+
latestError: new Error(errorMsg),
265+
});
266+
}
252267
}
253268
},
254269
[],

client/src/components/AuthDebugger.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AlertCircle } from "lucide-react";
55
import { AuthDebuggerState, EMPTY_DEBUGGER_STATE } from "../lib/auth-types";
66
import { OAuthFlowProgress } from "./OAuthFlowProgress";
77
import { OAuthStateMachine } from "../lib/oauth-state-machine";
8+
import { SESSION_KEYS } from "../lib/constants";
89

910
export interface AuthDebuggerProps {
1011
serverUrl: string;
@@ -141,6 +142,11 @@ const AuthDebugger = ({
141142
currentState.oauthStep === "authorization_code" &&
142143
currentState.authorizationUrl
143144
) {
145+
// Store the current auth state before redirecting
146+
sessionStorage.setItem(
147+
SESSION_KEYS.AUTH_DEBUGGER_STATE,
148+
JSON.stringify(currentState)
149+
);
144150
// Open the authorization URL automatically
145151
window.location.href = currentState.authorizationUrl;
146152
break;

client/src/components/OAuthDebugCallback.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ interface OAuthCallbackProps {
99
onConnect: ({
1010
authorizationCode,
1111
errorMsg,
12+
restoredState,
1213
}: {
1314
authorizationCode?: string;
1415
errorMsg?: string;
16+
restoredState?: any;
1517
}) => void;
1618
}
1719

@@ -34,6 +36,19 @@ const OAuthDebugCallback = ({ onConnect }: OAuthCallbackProps) => {
3436
}
3537

3638
const serverUrl = sessionStorage.getItem(SESSION_KEYS.SERVER_URL);
39+
40+
// Try to restore the auth state
41+
const storedState = sessionStorage.getItem(SESSION_KEYS.AUTH_DEBUGGER_STATE);
42+
let restoredState = null;
43+
if (storedState) {
44+
try {
45+
restoredState = JSON.parse(storedState);
46+
// Clean up the stored state
47+
sessionStorage.removeItem(SESSION_KEYS.AUTH_DEBUGGER_STATE);
48+
} catch (e) {
49+
console.error("Failed to parse stored auth state:", e);
50+
}
51+
}
3752

3853
// ServerURL isn't set, this can happen if we've opened the
3954
// authentication request in a new tab, so we don't have the same
@@ -50,8 +65,8 @@ const OAuthDebugCallback = ({ onConnect }: OAuthCallbackProps) => {
5065
}
5166

5267
// Instead of storing in sessionStorage, pass the code directly
53-
// to the auth state manager through onConnect
54-
onConnect({ authorizationCode: params.code });
68+
// to the auth state manager through onConnect, along with restored state
69+
onConnect({ authorizationCode: params.code, restoredState });
5570
};
5671

5772
handleCallback().finally(() => {

client/src/lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const SESSION_KEYS = {
77
TOKENS: "mcp_tokens",
88
CLIENT_INFORMATION: "mcp_client_information",
99
SERVER_METADATA: "mcp_server_metadata",
10+
AUTH_DEBUGGER_STATE: "mcp_auth_debugger_state",
1011
} as const;
1112

1213
// Generate server-specific session storage keys

0 commit comments

Comments
 (0)