mirror of
https://github.com/tinyhumansai/openhuman.git
synced 2026-07-30 23:14:37 +00:00
Refactor authentication flow to remove localStorage dependencies and improve state management
- Removed localStorage interactions for session tokens and user data in the TelegramLoginButton and authSlice, enhancing security and simplifying the codebase. - Updated the desktop deep link listener to eliminate localStorage usage for user data, streamlining the authentication process. - Adjusted the Redux store configuration to remove custom storage for backward compatibility, focusing on a cleaner state management approach. These changes enhance the application's architecture by centralizing state management and improving maintainability.
This commit is contained in:
@@ -8,28 +8,15 @@ const initialState: AuthState = {
|
||||
token: null,
|
||||
};
|
||||
|
||||
// Initialize from localStorage for backward compatibility
|
||||
if (typeof window !== 'undefined') {
|
||||
const legacyToken = localStorage.getItem('sessionToken');
|
||||
if (legacyToken) {
|
||||
initialState.token = legacyToken;
|
||||
}
|
||||
}
|
||||
|
||||
const authSlice = createSlice({
|
||||
name: 'auth',
|
||||
initialState,
|
||||
reducers: {
|
||||
setToken: (state, action: PayloadAction<string>) => {
|
||||
state.token = action.payload;
|
||||
// Also sync to localStorage for backward compatibility
|
||||
localStorage.setItem('sessionToken', action.payload);
|
||||
},
|
||||
clearToken: (state) => {
|
||||
state.token = null;
|
||||
// Also clear from localStorage for backward compatibility
|
||||
localStorage.removeItem('sessionToken');
|
||||
localStorage.removeItem('user');
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
+1
-70
@@ -12,76 +12,7 @@ const authPersistConfig = {
|
||||
whitelist: ['token'],
|
||||
};
|
||||
|
||||
// Custom storage that syncs with localStorage 'sessionToken' for backward compatibility
|
||||
// redux-persist stores data as JSON string with the state object
|
||||
const customStorage = {
|
||||
getItem: (key: string): Promise<string | null> => {
|
||||
return new Promise((resolve) => {
|
||||
// First check redux-persist storage (redux-persist adds 'persist:' prefix)
|
||||
const persistKey = `persist:${key}`;
|
||||
const persistData = localStorage.getItem(persistKey);
|
||||
|
||||
if (persistData) {
|
||||
try {
|
||||
const parsed = JSON.parse(persistData);
|
||||
// redux-persist format: { _persist: {...}, token: "..." }
|
||||
if (parsed && typeof parsed === 'object') {
|
||||
resolve(persistData);
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// Ignore parse errors
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to legacy sessionToken for backward compatibility
|
||||
const legacyToken = localStorage.getItem('sessionToken');
|
||||
if (legacyToken) {
|
||||
// Create redux-persist compatible format
|
||||
const fallbackData = JSON.stringify({
|
||||
token: legacyToken,
|
||||
_persist: { version: -1, rehydrated: true },
|
||||
});
|
||||
localStorage.setItem(persistKey, fallbackData);
|
||||
resolve(fallbackData);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(null);
|
||||
});
|
||||
},
|
||||
setItem: (key: string, value: string): Promise<void> => {
|
||||
return new Promise((resolve) => {
|
||||
const persistKey = `persist:${key}`;
|
||||
localStorage.setItem(persistKey, value);
|
||||
// Also sync to sessionToken for backward compatibility
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
if (parsed && typeof parsed === 'object' && parsed.token) {
|
||||
localStorage.setItem('sessionToken', parsed.token);
|
||||
}
|
||||
} catch {
|
||||
// Ignore parse errors
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
},
|
||||
removeItem: (key: string): Promise<void> => {
|
||||
return new Promise((resolve) => {
|
||||
const persistKey = `persist:${key}`;
|
||||
localStorage.removeItem(persistKey);
|
||||
localStorage.removeItem('sessionToken');
|
||||
resolve();
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
const authPersistConfigWithCustomStorage = {
|
||||
...authPersistConfig,
|
||||
storage: customStorage,
|
||||
};
|
||||
|
||||
const persistedAuthReducer = persistReducer(authPersistConfigWithCustomStorage, authReducer);
|
||||
const persistedAuthReducer = persistReducer(authPersistConfig, authReducer);
|
||||
|
||||
// Get logger only in dev mode
|
||||
let loggerMiddleware: unknown = undefined;
|
||||
|
||||
Reference in New Issue
Block a user