Auth: sanitize the initial state

merge-requests/588/head
Alex Gleason 2021-07-09 16:24:18 -05:00
rodzic d3db2e37e3
commit 8cc8a465c7
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
1 zmienionych plików z 19 dodań i 0 usunięć

Wyświetl plik

@ -83,6 +83,24 @@ const migrateLegacy = state => {
});
};
// Checks the state and makes it valid
const sanitizeState = state => {
return state.withMutations(state => {
// Remove invalid users, ensure ID match
state.update('users', ImmutableMap(), users => (
users.filter((user, id) => (
validUser(user) && user.get('id') === id
))
));
// Remove mismatched tokens
state.update('tokens', ImmutableMap(), tokens => (
tokens.filter((token, id) => (
validId(id) && token.get('access_token') === id
))
));
});
};
const persistAuth = state => localStorage.setItem('soapbox:auth', JSON.stringify(state.toJS()));
const persistSession = state => {
@ -102,6 +120,7 @@ const initialize = state => {
maybeShiftMe(state);
setSessionUser(state);
migrateLegacy(state);
sanitizeState(state);
persistState(state);
});
};