added clearStorage
This commit is contained in:
parent
752caf199a
commit
3a3d3724b5
1 changed files with 62 additions and 0 deletions
|
@ -70,6 +70,10 @@ export type UseMcpResult = {
|
||||||
* @returns Auth URL that can be used to manually open a new window
|
* @returns Auth URL that can be used to manually open a new window
|
||||||
*/
|
*/
|
||||||
authenticate: () => Promise<string | undefined>
|
authenticate: () => Promise<string | undefined>
|
||||||
|
/**
|
||||||
|
* Clear all localStorage items for this server
|
||||||
|
*/
|
||||||
|
clearStorage: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
type StoredState = {
|
type StoredState = {
|
||||||
|
@ -120,6 +124,44 @@ class BrowserOAuthClientProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears all storage items related to this server
|
||||||
|
* @returns The number of items cleared
|
||||||
|
*/
|
||||||
|
clearStorage(): number {
|
||||||
|
const prefix = `${this.storageKeyPrefix}_${this.serverUrlHash}`;
|
||||||
|
const keysToRemove = [];
|
||||||
|
|
||||||
|
// Find all keys that match the prefix
|
||||||
|
for (let i = 0; i < localStorage.length; i++) {
|
||||||
|
const key = localStorage.key(i);
|
||||||
|
if (key && key.startsWith(prefix)) {
|
||||||
|
keysToRemove.push(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also check for any state keys
|
||||||
|
for (let i = 0; i < localStorage.length; i++) {
|
||||||
|
const key = localStorage.key(i);
|
||||||
|
if (key && key.startsWith(`${this.storageKeyPrefix}:state_`)) {
|
||||||
|
// Load state to check if it's for this server
|
||||||
|
try {
|
||||||
|
const state = JSON.parse(localStorage.getItem(key) || '{}');
|
||||||
|
if (state.serverUrlHash === this.serverUrlHash) {
|
||||||
|
keysToRemove.push(key);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore JSON parse errors
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove all matching keys
|
||||||
|
keysToRemove.forEach(key => localStorage.removeItem(key));
|
||||||
|
|
||||||
|
return keysToRemove.length;
|
||||||
|
}
|
||||||
|
|
||||||
private hashString(str: string): string {
|
private hashString(str: string): string {
|
||||||
// Simple hash function for browser environments
|
// Simple hash function for browser environments
|
||||||
let hash = 0
|
let hash = 0
|
||||||
|
@ -722,6 +764,25 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
|
||||||
}
|
}
|
||||||
}, [disconnect])
|
}, [disconnect])
|
||||||
|
|
||||||
|
// Clear all localStorage items for this server
|
||||||
|
const clearStorage = useCallback(() => {
|
||||||
|
if (!authProviderRef.current) {
|
||||||
|
addLog('warn', 'Cannot clear storage: auth provider not initialized');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the provider's method to clear storage
|
||||||
|
const clearedCount = authProviderRef.current.clearStorage();
|
||||||
|
|
||||||
|
// Clear auth-related state in the hook
|
||||||
|
authUrlRef.current = undefined;
|
||||||
|
setAuthUrl(undefined);
|
||||||
|
metadataRef.current = undefined;
|
||||||
|
codeVerifierRef.current = undefined;
|
||||||
|
|
||||||
|
addLog('info', `Cleared ${clearedCount} storage items for server`);
|
||||||
|
}, [addLog]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
state,
|
state,
|
||||||
tools,
|
tools,
|
||||||
|
@ -732,6 +793,7 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
|
||||||
retry,
|
retry,
|
||||||
disconnect,
|
disconnect,
|
||||||
authenticate,
|
authenticate,
|
||||||
|
clearStorage,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue