The 1st decentralized social network for sharing when you're on the toilet. Post a "flush" today! Powered by the AT Protocol.
1// A utility file to handle browser storage robustly
2
3// Store data with both localStorage and sessionStorage for redundancy
4export function storeAuthData(key: string, value: string): boolean {
5 try {
6 // Clear any existing values first
7 sessionStorage.removeItem(key);
8 localStorage.removeItem(key);
9
10 // Store in both storages for redundancy
11 sessionStorage.setItem(key, value);
12 localStorage.setItem(`bsky_auth_${key}`, value); // Use a prefix to avoid conflicts
13
14 return true;
15 } catch (error) {
16 console.error(`Failed to store auth data for key ${key}:`, error);
17 return false;
18 }
19}
20
21// Retrieve data from sessionStorage first, fall back to localStorage
22export function retrieveAuthData(key: string): string | null {
23 try {
24 // Try sessionStorage first (preferred)
25 const sessionValue = sessionStorage.getItem(key);
26 if (sessionValue) {
27 return sessionValue;
28 }
29
30 // Fall back to localStorage if needed
31 const localValue = localStorage.getItem(`bsky_auth_${key}`);
32 if (localValue) {
33 console.log(`Retrieved auth data for ${key} from localStorage fallback`);
34 // Store it back in sessionStorage for next time
35 try {
36 sessionStorage.setItem(key, localValue);
37 } catch (e) {
38 console.warn('Could not restore value to sessionStorage:', e);
39 }
40 return localValue;
41 }
42
43 // Nothing found
44 return null;
45 } catch (error) {
46 console.error(`Failed to retrieve auth data for key ${key}:`, error);
47 return null;
48 }
49}
50
51// Clear auth data from both storages
52export function clearAuthData(key: string): void {
53 try {
54 sessionStorage.removeItem(key);
55 localStorage.removeItem(`bsky_auth_${key}`);
56 } catch (error) {
57 console.error(`Failed to clear auth data for key ${key}:`, error);
58 }
59}