pleroma/app/soapbox/reducers/auth.js

38 lines
1.3 KiB
JavaScript
Raw Normal View History

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,
SWITCH_ACCOUNT,
2020-04-05 16:39:22 -07:00
} from '../actions/auth';
import { Map as ImmutableMap, fromJS } from 'immutable';
2020-04-05 14:54:51 -07:00
const initialState = ImmutableMap({
2020-04-05 16:39:22 -07:00
app: ImmutableMap(JSON.parse(localStorage.getItem('soapbox:auth:app'))),
users: fromJS(JSON.parse(localStorage.getItem('soapbox:auth:users'))),
me: localStorage.getItem('soapbox:auth:me'),
2020-04-05 14:54:51 -07:00
});
export default function auth(state = initialState, action) {
switch(action.type) {
case AUTH_APP_CREATED:
2020-04-05 16:39:22 -07:00
localStorage.setItem('soapbox:auth:app', JSON.stringify(action.app)); // TODO: Better persistence
2020-04-05 14:54:51 -07:00
return state.set('app', ImmutableMap(action.app));
2020-04-05 16:39:22 -07:00
case AUTH_APP_AUTHORIZED:
const merged = state.get('app').merge(ImmutableMap(action.app));
localStorage.setItem('soapbox:auth:app', JSON.stringify(merged)); // TODO: Better persistence
return state.set('app', merged);
2020-04-05 14:54:51 -07:00
case AUTH_LOGGED_IN:
return state.set('user', ImmutableMap(action.user));
2020-04-11 12:41:13 -07:00
case AUTH_LOGGED_OUT:
localStorage.removeItem('soapbox:auth:user');
2020-06-05 13:43:03 -07:00
return state.set('user', ImmutableMap());
case SWITCH_ACCOUNT:
localStorage.setItem('soapbox:auth:me', action.accountId);
location.reload();
return state;
2020-04-05 14:54:51 -07:00
default:
return state;
}
};