Initialize auth refactor

This commit is contained in:
Alex Gleason 2021-03-29 19:42:14 -05:00
parent a055431c24
commit aa7e62e8c0
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
4 changed files with 79 additions and 72 deletions

View file

@ -1,7 +0,0 @@
export const INIT_STORE = 'INIT_STORE';
export function initStore() {
return {
type: INIT_STORE,
};
}

View file

@ -14,7 +14,6 @@ import { ScrollContext } from 'react-router-scroll-4';
import UI from '../features/ui';
// import Introduction from '../features/introduction';
import { fetchCustomEmojis } from '../actions/custom_emojis';
import { initStore } from '../actions/store';
import { preload } from '../actions/preload';
import { IntlProvider } from 'react-intl';
import ErrorBoundary from '../components/error_boundary';
@ -31,7 +30,6 @@ const validLocale = locale => Object.keys(messages).includes(locale);
export const store = configureStore();
store.dispatch(initStore());
store.dispatch(preload());
store.dispatch(fetchMe());
store.dispatch(fetchInstance());

View file

@ -1,9 +1,9 @@
import reducer from '../auth';
import { Map as ImmutableMap, fromJS } from 'immutable';
import { INIT_STORE } from 'soapbox/actions/store';
import {
AUTH_APP_CREATED,
AUTH_LOGGED_IN,
AUTH_LOGGED_OUT,
VERIFY_CREDENTIALS_SUCCESS,
VERIFY_CREDENTIALS_FAIL,
SWITCH_ACCOUNT,
@ -19,22 +19,6 @@ describe('auth reducer', () => {
}));
});
describe('INIT_STORE', () => {
it('sets `me` to the next available user if blank', () => {
const state = fromJS({
me: null,
users: {
'1234': { id: '1234', access_token: 'ABCDEFG' },
'5678': { id: '5678', access_token: 'HIJKLMN' },
},
});
const action = { type: INIT_STORE };
const result = reducer(state, action);
expect(result.get('me')).toEqual('1234');
});
});
describe('AUTH_APP_CREATED', () => {
it('should copy in the app', () => {
const token = { token_type: 'Bearer', access_token: 'ABCDEFG' };
@ -78,6 +62,40 @@ describe('auth reducer', () => {
});
});
describe('AUTH_LOGGED_OUT', () => {
it('deletes the user', () => {
const action = { type: AUTH_LOGGED_OUT, accountId: '1234' };
const state = fromJS({
users: {
'1234': { id: '1234', access_token: 'ABCDEFG' },
'5678': { id: '5678', access_token: 'HIJKLMN' },
},
});
const expected = fromJS({
'5678': { id: '5678', access_token: 'HIJKLMN' },
});
const result = reducer(state, action);
expect(result.get('users')).toEqual(expected);
});
it('sets `me` to the next available user', () => {
const state = fromJS({
me: '1234',
users: {
'1234': { id: '1234', access_token: 'ABCDEFG' },
'5678': { id: '5678', access_token: 'HIJKLMN' },
},
});
const action = { type: AUTH_LOGGED_OUT, accountId: '1234' };
const result = reducer(state, action);
expect(result.get('me')).toEqual('5678');
});
});
describe('VERIFY_CREDENTIALS_SUCCESS', () => {
it('should import the user', () => {
const action = {

View file

@ -1,4 +1,3 @@
import { INIT_STORE } from '../actions/store';
import {
AUTH_APP_CREATED,
AUTH_LOGGED_IN,
@ -18,23 +17,6 @@ const defaultState = ImmutableMap({
});
const localState = fromJS(JSON.parse(localStorage.getItem('soapbox:auth')));
const initialState = defaultState.merge(localState);
const importToken = (state, token) => {
return state.setIn(['tokens', token.access_token], fromJS(token));
};
const importCredentials = (state, token, account) => {
return state.withMutations(state => {
state.setIn(['users', account.id], ImmutableMap({
id: account.id,
access_token: token,
}));
state.setIn(['tokens', token, 'account'], account.id);
state.update('me', null, me => me || account.id);
upgradeLegacyId(state, account);
});
};
// If `me` doesn't match an existing user, attempt to shift it.
const maybeShiftMe = state => {
@ -42,26 +24,20 @@ const maybeShiftMe = state => {
const me = state.get('me');
if (!users.get(me)) {
return state.set('me', users.first(ImmutableMap()).get('id'));
return state.set('me', users.first(ImmutableMap()).get('id', null));
} else {
return state;
}
};
const deleteToken = (state, token) => {
return state.withMutations(state => {
state.update('tokens', ImmutableMap(), tokens => tokens.delete(token));
state.update('users', ImmutableMap(), users => users.filterNot(user => user.get('access_token') === token));
maybeShiftMe(state);
});
};
const deleteUser = (state, accountId) => {
return state.withMutations(state => {
state.update('users', ImmutableMap(), users => users.delete(accountId));
state.update('tokens', ImmutableMap(), tokens => tokens.filterNot(token => token.get('account') === accountId));
maybeShiftMe(state);
});
const setSessionUser = state => {
const sessionUser = sessionStorage.getItem('soapbox:auth:me');
if (sessionUser) {
return state.set('me', sessionUser);
} else {
sessionStorage.setItem('soapbox:auth:me', state.get('me', null));
return state;
}
};
// Upgrade the initial state
@ -85,6 +61,20 @@ const migrateLegacy = state => {
});
};
const initialize = state => {
return state.withMutations(state => {
maybeShiftMe(state);
setSessionUser(state);
migrateLegacy(state);
});
};
const initialState = defaultState.merge(localState).withMutations(initialize);
const importToken = (state, token) => {
return state.setIn(['tokens', token.access_token], fromJS(token));
};
// Upgrade the `_legacy` placeholder ID with a real account
const upgradeLegacyId = (state, account) => {
if (localState) return state;
@ -96,28 +86,36 @@ const upgradeLegacyId = (state, account) => {
// By this point it's probably safe, but we'll leave it just in case.
};
const setSessionUser = state => {
const sessionUser = sessionStorage.getItem('soapbox:auth:me');
if (sessionUser) {
return state.set('me', sessionUser);
} else {
sessionStorage.setItem('soapbox:auth:me', state.get('me'));
return state;
}
const importCredentials = (state, token, account) => {
return state.withMutations(state => {
state.setIn(['users', account.id], ImmutableMap({
id: account.id,
access_token: token,
}));
state.setIn(['tokens', token, 'account'], account.id);
state.update('me', null, me => me || account.id);
upgradeLegacyId(state, account);
});
};
const initialize = state => {
const deleteToken = (state, token) => {
return state.withMutations(state => {
setSessionUser(state);
state.update('tokens', ImmutableMap(), tokens => tokens.delete(token));
state.update('users', ImmutableMap(), users => users.filterNot(user => user.get('access_token') === token));
maybeShiftMe(state);
});
};
const deleteUser = (state, accountId) => {
return state.withMutations(state => {
state.update('users', ImmutableMap(), users => users.delete(accountId));
state.update('tokens', ImmutableMap(), tokens => tokens.filterNot(token => token.get('account') === accountId));
maybeShiftMe(state);
migrateLegacy(state);
});
};
const reducer = (state, action) => {
switch(action.type) {
case INIT_STORE:
return initialize(state);
case AUTH_APP_CREATED:
return state.set('app', fromJS(action.app));
case AUTH_APP_AUTHORIZED: