Initialize auth refactor
This commit is contained in:
parent
a055431c24
commit
aa7e62e8c0
4 changed files with 79 additions and 72 deletions
|
@ -1,7 +0,0 @@
|
||||||
export const INIT_STORE = 'INIT_STORE';
|
|
||||||
|
|
||||||
export function initStore() {
|
|
||||||
return {
|
|
||||||
type: INIT_STORE,
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -14,7 +14,6 @@ import { ScrollContext } from 'react-router-scroll-4';
|
||||||
import UI from '../features/ui';
|
import UI from '../features/ui';
|
||||||
// import Introduction from '../features/introduction';
|
// import Introduction from '../features/introduction';
|
||||||
import { fetchCustomEmojis } from '../actions/custom_emojis';
|
import { fetchCustomEmojis } from '../actions/custom_emojis';
|
||||||
import { initStore } from '../actions/store';
|
|
||||||
import { preload } from '../actions/preload';
|
import { preload } from '../actions/preload';
|
||||||
import { IntlProvider } from 'react-intl';
|
import { IntlProvider } from 'react-intl';
|
||||||
import ErrorBoundary from '../components/error_boundary';
|
import ErrorBoundary from '../components/error_boundary';
|
||||||
|
@ -31,7 +30,6 @@ const validLocale = locale => Object.keys(messages).includes(locale);
|
||||||
|
|
||||||
export const store = configureStore();
|
export const store = configureStore();
|
||||||
|
|
||||||
store.dispatch(initStore());
|
|
||||||
store.dispatch(preload());
|
store.dispatch(preload());
|
||||||
store.dispatch(fetchMe());
|
store.dispatch(fetchMe());
|
||||||
store.dispatch(fetchInstance());
|
store.dispatch(fetchInstance());
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import reducer from '../auth';
|
import reducer from '../auth';
|
||||||
import { Map as ImmutableMap, fromJS } from 'immutable';
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
||||||
import { INIT_STORE } from 'soapbox/actions/store';
|
|
||||||
import {
|
import {
|
||||||
AUTH_APP_CREATED,
|
AUTH_APP_CREATED,
|
||||||
AUTH_LOGGED_IN,
|
AUTH_LOGGED_IN,
|
||||||
|
AUTH_LOGGED_OUT,
|
||||||
VERIFY_CREDENTIALS_SUCCESS,
|
VERIFY_CREDENTIALS_SUCCESS,
|
||||||
VERIFY_CREDENTIALS_FAIL,
|
VERIFY_CREDENTIALS_FAIL,
|
||||||
SWITCH_ACCOUNT,
|
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', () => {
|
describe('AUTH_APP_CREATED', () => {
|
||||||
it('should copy in the app', () => {
|
it('should copy in the app', () => {
|
||||||
const token = { token_type: 'Bearer', access_token: 'ABCDEFG' };
|
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', () => {
|
describe('VERIFY_CREDENTIALS_SUCCESS', () => {
|
||||||
it('should import the user', () => {
|
it('should import the user', () => {
|
||||||
const action = {
|
const action = {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import { INIT_STORE } from '../actions/store';
|
|
||||||
import {
|
import {
|
||||||
AUTH_APP_CREATED,
|
AUTH_APP_CREATED,
|
||||||
AUTH_LOGGED_IN,
|
AUTH_LOGGED_IN,
|
||||||
|
@ -18,23 +17,6 @@ const defaultState = ImmutableMap({
|
||||||
});
|
});
|
||||||
|
|
||||||
const localState = fromJS(JSON.parse(localStorage.getItem('soapbox:auth')));
|
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.
|
// If `me` doesn't match an existing user, attempt to shift it.
|
||||||
const maybeShiftMe = state => {
|
const maybeShiftMe = state => {
|
||||||
|
@ -42,26 +24,20 @@ const maybeShiftMe = state => {
|
||||||
const me = state.get('me');
|
const me = state.get('me');
|
||||||
|
|
||||||
if (!users.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 {
|
} else {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteToken = (state, token) => {
|
const setSessionUser = state => {
|
||||||
return state.withMutations(state => {
|
const sessionUser = sessionStorage.getItem('soapbox:auth:me');
|
||||||
state.update('tokens', ImmutableMap(), tokens => tokens.delete(token));
|
if (sessionUser) {
|
||||||
state.update('users', ImmutableMap(), users => users.filterNot(user => user.get('access_token') === token));
|
return state.set('me', sessionUser);
|
||||||
maybeShiftMe(state);
|
} else {
|
||||||
});
|
sessionStorage.setItem('soapbox:auth:me', state.get('me', null));
|
||||||
};
|
return 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);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Upgrade the initial 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
|
// Upgrade the `_legacy` placeholder ID with a real account
|
||||||
const upgradeLegacyId = (state, account) => {
|
const upgradeLegacyId = (state, account) => {
|
||||||
if (localState) return state;
|
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.
|
// By this point it's probably safe, but we'll leave it just in case.
|
||||||
};
|
};
|
||||||
|
|
||||||
const setSessionUser = state => {
|
const importCredentials = (state, token, account) => {
|
||||||
const sessionUser = sessionStorage.getItem('soapbox:auth:me');
|
return state.withMutations(state => {
|
||||||
if (sessionUser) {
|
state.setIn(['users', account.id], ImmutableMap({
|
||||||
return state.set('me', sessionUser);
|
id: account.id,
|
||||||
} else {
|
access_token: token,
|
||||||
sessionStorage.setItem('soapbox:auth:me', state.get('me'));
|
}));
|
||||||
return state;
|
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 => {
|
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);
|
maybeShiftMe(state);
|
||||||
migrateLegacy(state);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const reducer = (state, action) => {
|
const reducer = (state, action) => {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case INIT_STORE:
|
|
||||||
return initialize(state);
|
|
||||||
case AUTH_APP_CREATED:
|
case AUTH_APP_CREATED:
|
||||||
return state.set('app', fromJS(action.app));
|
return state.set('app', fromJS(action.app));
|
||||||
case AUTH_APP_AUTHORIZED:
|
case AUTH_APP_AUTHORIZED:
|
||||||
|
|
Loading…
Reference in a new issue