kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
Refactor auth reducer localStorage
rodzic
60a3a5b403
commit
0daa95646e
|
@ -331,9 +331,9 @@ export function authAppAuthorized(app) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function authLoggedIn(user) {
|
export function authLoggedIn(token) {
|
||||||
return {
|
return {
|
||||||
type: AUTH_LOGGED_IN,
|
type: AUTH_LOGGED_IN,
|
||||||
user,
|
token,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,31 +7,40 @@ import {
|
||||||
} from '../actions/auth';
|
} from '../actions/auth';
|
||||||
import { Map as ImmutableMap, fromJS } from 'immutable';
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
||||||
|
|
||||||
const initialState = ImmutableMap({
|
const defaultState = ImmutableMap({
|
||||||
app: ImmutableMap(JSON.parse(localStorage.getItem('soapbox:auth:app'))),
|
app: ImmutableMap(),
|
||||||
users: fromJS(JSON.parse(localStorage.getItem('soapbox:auth:users'))),
|
user: ImmutableMap(),
|
||||||
me: localStorage.getItem('soapbox:auth:me'),
|
users: ImmutableMap(),
|
||||||
|
me: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
export default function auth(state = initialState, action) {
|
const localState = fromJS(JSON.parse(localStorage.getItem('soapbox:auth')));
|
||||||
|
const initialState = defaultState.merge(localState);
|
||||||
|
|
||||||
|
const reducer = (state, action) => {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case AUTH_APP_CREATED:
|
case AUTH_APP_CREATED:
|
||||||
localStorage.setItem('soapbox:auth:app', JSON.stringify(action.app)); // TODO: Better persistence
|
return state.set('app', fromJS(action.app));
|
||||||
return state.set('app', ImmutableMap(action.app));
|
|
||||||
case AUTH_APP_AUTHORIZED:
|
case AUTH_APP_AUTHORIZED:
|
||||||
const merged = state.get('app').merge(ImmutableMap(action.app));
|
return state.update('app', ImmutableMap(), app => app.merge(fromJS(action.app)));
|
||||||
localStorage.setItem('soapbox:auth:app', JSON.stringify(merged)); // TODO: Better persistence
|
|
||||||
return state.set('app', merged);
|
|
||||||
case AUTH_LOGGED_IN:
|
case AUTH_LOGGED_IN:
|
||||||
return state.set('user', ImmutableMap(action.user));
|
return state.set('user', fromJS(action.token));
|
||||||
case AUTH_LOGGED_OUT:
|
case AUTH_LOGGED_OUT:
|
||||||
localStorage.removeItem('soapbox:auth:user');
|
|
||||||
return state.set('user', ImmutableMap());
|
return state.set('user', ImmutableMap());
|
||||||
case SWITCH_ACCOUNT:
|
case SWITCH_ACCOUNT:
|
||||||
localStorage.setItem('soapbox:auth:me', action.accountId);
|
return state.set('me', action.accountId);
|
||||||
location.reload();
|
|
||||||
return state;
|
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default function auth(state = initialState, action) {
|
||||||
|
state = reducer(state, action);
|
||||||
|
localStorage.setItem('soapbox:auth', JSON.stringify(state.toJS()));
|
||||||
|
|
||||||
|
if (action.type === SWITCH_ACCOUNT) {
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
return state;
|
||||||
|
};
|
||||||
|
|
Ładowanie…
Reference in New Issue