2020-04-05 16:39:22 -07:00
|
|
|
import {
|
|
|
|
AUTH_APP_CREATED,
|
|
|
|
AUTH_LOGGED_IN,
|
|
|
|
AUTH_APP_AUTHORIZED,
|
|
|
|
} from '../actions/auth';
|
2020-04-05 14:54:51 -07:00
|
|
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
2020-04-05 16:39:22 -07:00
|
|
|
app: ImmutableMap(JSON.parse(localStorage.getItem('soapbox:auth:app'))),
|
|
|
|
user: ImmutableMap(JSON.parse(localStorage.getItem('soapbox:auth:user'))),
|
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:
|
2020-04-05 16:39:22 -07:00
|
|
|
localStorage.setItem('soapbox:auth:user', JSON.stringify(action.user)); // TODO: Better persistence
|
2020-04-05 14:54:51 -07:00
|
|
|
return state.set('user', ImmutableMap(action.user));
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|