2020-04-05 16:39:22 -07:00
|
|
|
import {
|
|
|
|
AUTH_APP_CREATED,
|
|
|
|
AUTH_LOGGED_IN,
|
|
|
|
AUTH_APP_AUTHORIZED,
|
2020-04-11 12:41:13 -07:00
|
|
|
AUTH_LOGGED_OUT,
|
2021-03-23 17:06:55 -07:00
|
|
|
SWITCH_ACCOUNT,
|
2021-03-23 22:05:06 -07:00
|
|
|
VERIFY_CREDENTIALS_SUCCESS,
|
2021-03-24 12:15:36 -07:00
|
|
|
VERIFY_CREDENTIALS_FAIL,
|
2020-04-05 16:39:22 -07:00
|
|
|
} from '../actions/auth';
|
2021-03-23 19:15:47 -07:00
|
|
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
2020-04-05 14:54:51 -07:00
|
|
|
|
2021-03-23 19:52:08 -07:00
|
|
|
const defaultState = ImmutableMap({
|
|
|
|
app: ImmutableMap(),
|
2021-03-24 13:01:10 -07:00
|
|
|
users: ImmutableMap(),
|
2021-03-23 22:05:06 -07:00
|
|
|
tokens: ImmutableMap(),
|
2021-03-23 19:52:08 -07:00
|
|
|
me: null,
|
2020-04-05 14:54:51 -07:00
|
|
|
});
|
|
|
|
|
2021-03-23 19:52:08 -07:00
|
|
|
const localState = fromJS(JSON.parse(localStorage.getItem('soapbox:auth')));
|
|
|
|
const initialState = defaultState.merge(localState);
|
|
|
|
|
2021-03-23 22:05:06 -07:00
|
|
|
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);
|
2021-03-24 14:18:14 -07:00
|
|
|
upgradeLegacyId(state, account);
|
2021-03-23 22:05:06 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-03-24 12:15:36 -07:00
|
|
|
// If `me` doesn't match an existing user, attempt to shift it.
|
|
|
|
const maybeShiftMe = state => {
|
|
|
|
const users = state.get('users', ImmutableMap());
|
|
|
|
const me = state.get('me');
|
|
|
|
|
|
|
|
if (!users.get(me)) {
|
|
|
|
return state.set('me', users.first(ImmutableMap()).get('id'));
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const importFailedToken = (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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-03-24 14:18:14 -07:00
|
|
|
// Upgrade the initial state
|
|
|
|
const migrateLegacy = state => {
|
|
|
|
if (localState) return state;
|
|
|
|
return state.withMutations(state => {
|
|
|
|
const app = fromJS(JSON.parse(localStorage.getItem('soapbox:auth:app')));
|
|
|
|
const user = fromJS(JSON.parse(localStorage.getItem('soapbox:auth:user')));
|
|
|
|
state.set('me', '_legacy'); // Placeholder account ID
|
|
|
|
state.set('app', app);
|
|
|
|
state.set('tokens', ImmutableMap({
|
|
|
|
[user.get('access_token')]: user.set('account', '_legacy'),
|
|
|
|
}));
|
|
|
|
state.set('users', ImmutableMap({
|
|
|
|
'_legacy': ImmutableMap({
|
|
|
|
id: '_legacy',
|
|
|
|
access_token: user.get('access_token'),
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Upgrade the `_legacy` placeholder ID with a real account
|
|
|
|
const upgradeLegacyId = (state, account) => {
|
|
|
|
if (localState) return state;
|
|
|
|
return state.withMutations(state => {
|
|
|
|
state.update('me', null, me => me === '_legacy' ? account.id : me);
|
|
|
|
state.deleteIn(['users', '_legacy']);
|
|
|
|
});
|
|
|
|
// TODO: Delete `soapbox:auth:app` and `soapbox:auth:user` localStorage?
|
|
|
|
// By this point it's probably safe, but we'll leave it just in case.
|
|
|
|
};
|
|
|
|
|
|
|
|
const initialize = state => {
|
|
|
|
return state.withMutations(state => {
|
|
|
|
maybeShiftMe(state);
|
|
|
|
migrateLegacy(state);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-03-23 19:52:08 -07:00
|
|
|
const reducer = (state, action) => {
|
2020-04-05 14:54:51 -07:00
|
|
|
switch(action.type) {
|
2021-03-24 14:18:14 -07:00
|
|
|
case '@@INIT':
|
|
|
|
return initialize(state);
|
2020-04-05 14:54:51 -07:00
|
|
|
case AUTH_APP_CREATED:
|
2021-03-23 19:52:08 -07:00
|
|
|
return state.set('app', fromJS(action.app));
|
2020-04-05 16:39:22 -07:00
|
|
|
case AUTH_APP_AUTHORIZED:
|
2021-03-23 19:52:08 -07:00
|
|
|
return state.update('app', ImmutableMap(), app => app.merge(fromJS(action.app)));
|
2020-04-05 14:54:51 -07:00
|
|
|
case AUTH_LOGGED_IN:
|
2021-03-23 22:05:06 -07:00
|
|
|
return importToken(state, action.token);
|
2020-04-11 12:41:13 -07:00
|
|
|
case AUTH_LOGGED_OUT:
|
2020-06-05 13:43:03 -07:00
|
|
|
return state.set('user', ImmutableMap());
|
2021-03-23 22:05:06 -07:00
|
|
|
case VERIFY_CREDENTIALS_SUCCESS:
|
|
|
|
return importCredentials(state, action.token, action.account);
|
2021-03-24 12:15:36 -07:00
|
|
|
case VERIFY_CREDENTIALS_FAIL:
|
|
|
|
return importFailedToken(state, action.token);
|
2021-03-23 17:06:55 -07:00
|
|
|
case SWITCH_ACCOUNT:
|
2021-03-23 19:52:08 -07:00
|
|
|
return state.set('me', action.accountId);
|
2020-04-05 14:54:51 -07:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
2021-03-23 19:52:08 -07:00
|
|
|
|
2021-03-24 12:15:36 -07:00
|
|
|
export default function auth(oldState = initialState, action) {
|
|
|
|
const state = reducer(oldState, action);
|
2021-03-23 19:52:08 -07:00
|
|
|
localStorage.setItem('soapbox:auth', JSON.stringify(state.toJS()));
|
|
|
|
|
2021-03-24 12:15:36 -07:00
|
|
|
// Reload the page when the current user changes.
|
|
|
|
if (state.get('me') !== oldState.get('me')) {
|
2021-03-23 19:52:08 -07:00
|
|
|
location.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|