Skip to content

Commit 02cfb47

Browse files
committed
Extract session storage keys into constants
1 parent 23f89e4 commit 02cfb47

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

client/src/components/OAuthCallback.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { useEffect } from 'react';
22
import { handleOAuthCallback } from '../lib/auth';
3+
import { SESSION_KEYS } from '../lib/constants';
34

45
const OAuthCallback = () => {
56
useEffect(() => {
67
const handleCallback = async () => {
78
const params = new URLSearchParams(window.location.search);
89
const code = params.get('code');
9-
const serverUrl = sessionStorage.getItem('mcp_server_url');
10+
const serverUrl = sessionStorage.getItem(SESSION_KEYS.SERVER_URL);
1011

1112
if (!code || !serverUrl) {
1213
console.error('Missing code or server URL');
@@ -17,7 +18,7 @@ const OAuthCallback = () => {
1718
try {
1819
const accessToken = await handleOAuthCallback(serverUrl, code);
1920
// Store the access token for future use
20-
sessionStorage.setItem('mcp_access_token', accessToken);
21+
sessionStorage.setItem(SESSION_KEYS.ACCESS_TOKEN, accessToken);
2122
// Redirect back to the main app
2223
window.location.href = '/';
2324
} catch (error) {

client/src/lib/auth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pkceChallenge from 'pkce-challenge';
2+
import { SESSION_KEYS } from './constants';
23

34
export interface OAuthMetadata {
45
authorization_endpoint: string;
@@ -36,7 +37,7 @@ export async function startOAuthFlow(serverUrl: string): Promise<string> {
3637
const codeChallenge = challenge.code_challenge;
3738

3839
// Store code verifier for later use
39-
sessionStorage.setItem('mcp_code_verifier', codeVerifier);
40+
sessionStorage.setItem(SESSION_KEYS.CODE_VERIFIER, codeVerifier);
4041

4142
// Discover OAuth endpoints
4243
const metadata = await discoverOAuthMetadata(serverUrl);
@@ -53,7 +54,7 @@ export async function startOAuthFlow(serverUrl: string): Promise<string> {
5354

5455
export async function handleOAuthCallback(serverUrl: string, code: string): Promise<string> {
5556
// Get stored code verifier
56-
const codeVerifier = sessionStorage.getItem('mcp_code_verifier');
57+
const codeVerifier = sessionStorage.getItem(SESSION_KEYS.CODE_VERIFIER);
5758
if (!codeVerifier) {
5859
throw new Error('No code verifier found');
5960
}

client/src/lib/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// OAuth-related session storage keys
2+
export const SESSION_KEYS = {
3+
CODE_VERIFIER: 'mcp_code_verifier',
4+
SERVER_URL: 'mcp_server_url',
5+
ACCESS_TOKEN: 'mcp_access_token',
6+
} as const;

client/src/lib/hooks/useConnection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { useState } from "react";
1414
import { toast } from "react-toastify";
1515
import { z } from "zod";
1616
import { startOAuthFlow } from "../auth";
17+
import { SESSION_KEYS } from "../constants";
1718
import { Notification, StdErrNotificationSchema } from "../notificationTypes";
1819

1920
const DEFAULT_REQUEST_TIMEOUT_MSEC = 10000;
@@ -167,7 +168,7 @@ export function useConnection({
167168
console.error("Failed to connect to MCP server:", error);
168169
if (error instanceof SseError && error.code === 401) {
169170
// Store the server URL for the callback handler
170-
sessionStorage.setItem('mcp_server_url', sseUrl);
171+
sessionStorage.setItem(SESSION_KEYS.SERVER_URL, sseUrl);
171172
const redirectUrl = await startOAuthFlow(sseUrl);
172173
window.location.href = redirectUrl;
173174
}

0 commit comments

Comments
 (0)