Rename storage to localStorage in context

This commit is contained in:
Chi Vinh Le
2018-02-01 13:28:28 +01:00
parent 118301c2b4
commit 91e08cb117
8 changed files with 46 additions and 42 deletions
+17 -13
View File
@@ -10,7 +10,7 @@ import jwtDecode from 'jwt-decode';
export const handleLogin = (email, password, recaptchaResponse) => (
dispatch,
_,
{ rest, client, storage }
{ rest, client, localStorage }
) => {
dispatch({ type: actions.LOGIN_REQUEST });
@@ -31,8 +31,8 @@ export const handleLogin = (email, password, recaptchaResponse) => (
return rest('/auth/local', params)
.then(({ user, token }) => {
if (!user) {
if (!bowser.safari && !bowser.ios && storage) {
storage.removeItem('token');
if (!bowser.safari && !bowser.ios && localStorage) {
localStorage.removeItem('token');
}
return dispatch(checkLoginFailure('not logged in'));
}
@@ -121,13 +121,17 @@ const checkLoginFailure = error => ({
error,
});
export const checkLogin = () => (dispatch, _, { rest, client, storage }) => {
export const checkLogin = () => (
dispatch,
_,
{ rest, client, localStorage }
) => {
dispatch(checkLoginRequest());
return rest('/auth')
.then(({ user }) => {
if (!user) {
if (!bowser.safari && !bowser.ios && storage) {
storage.removeItem('token');
if (!bowser.safari && !bowser.ios && localStorage) {
localStorage.removeItem('token');
}
return dispatch(checkLoginFailure('not logged in'));
}
@@ -148,10 +152,10 @@ export const checkLogin = () => (dispatch, _, { rest, client, storage }) => {
// LOGOUT
//==============================================================================
export const logout = () => (dispatch, _, { rest, client, storage }) => {
export const logout = () => (dispatch, _, { rest, client, localStorage }) => {
return rest('/auth', { method: 'DELETE' }).then(() => {
if (storage) {
storage.removeItem('token');
if (localStorage) {
localStorage.removeItem('token');
}
// Reset the websocket.
@@ -165,10 +169,10 @@ export const logout = () => (dispatch, _, { rest, client, storage }) => {
// AUTH TOKEN
//==============================================================================
export const handleAuthToken = token => (dispatch, _, { storage }) => {
if (storage) {
storage.setItem('exp', jwtDecode(token).exp);
storage.setItem('token', token);
export const handleAuthToken = token => (dispatch, _, { localStorage }) => {
if (localStorage) {
localStorage.setItem('exp', jwtDecode(token).exp);
localStorage.setItem('token', token);
}
dispatch({ type: 'HANDLE_AUTH_TOKEN' });
};
+3 -3
View File
@@ -4,10 +4,10 @@ export const toggleModal = open => ({ type: actions.TOGGLE_MODAL, open });
export const singleView = () => ({ type: actions.SINGLE_VIEW });
// hide shortcuts note
export const hideShortcutsNote = () => (dispatch, _, { storage }) => {
export const hideShortcutsNote = () => (dispatch, _, { localStorage }) => {
try {
if (storage) {
storage.setItem('coral:shortcutsNote', 'hide');
if (localStorage) {
localStorage.setItem('coral:shortcutsNote', 'hide');
}
} catch (e) {
// above will fail in Safari private mode
+2 -2
View File
@@ -14,8 +14,8 @@ import { hideShortcutsNote } from './actions/moderation';
smoothscroll.polyfill();
function init({ store, storage }) {
if (storage && storage.getItem('coral:shortcutsNote') === 'hide') {
function init({ store, localStorage }) {
if (localStorage && localStorage.getItem('coral:shortcutsNote') === 'hide') {
store.dispatch(hideShortcutsNote());
}
}
+12 -12
View File
@@ -90,10 +90,10 @@ const signInFailure = error => ({
// AUTH TOKEN
//==============================================================================
export const handleAuthToken = token => (dispatch, _, { storage }) => {
if (storage) {
storage.setItem('exp', jwtDecode(token).exp);
storage.setItem('token', token);
export const handleAuthToken = token => (dispatch, _, { localStorage }) => {
if (localStorage) {
localStorage.setItem('exp', jwtDecode(token).exp);
localStorage.setItem('token', token);
}
dispatch({ type: 'HANDLE_AUTH_TOKEN' });
@@ -260,12 +260,12 @@ export const fetchForgotPassword = email => (dispatch, getState, { rest }) => {
export const logout = () => async (
dispatch,
_,
{ rest, client, pym, storage }
{ rest, client, pym, localStorage }
) => {
await rest('/auth', { method: 'DELETE' });
if (storage) {
storage.removeItem('token');
if (localStorage) {
localStorage.removeItem('token');
}
// Reset the websocket.
@@ -296,14 +296,14 @@ const ErrNotLoggedIn = new Error('Not logged in');
export const checkLogin = () => (
dispatch,
_,
{ rest, client, pym, storage }
{ rest, client, pym, localStorage }
) => {
dispatch(checkLoginRequest());
rest('/auth')
.then(result => {
if (!result.user) {
if (storage) {
storage.removeItem('token');
if (localStorage) {
localStorage.removeItem('token');
}
throw ErrNotLoggedIn;
}
@@ -326,9 +326,9 @@ export const checkLogin = () => (
if (error !== ErrNotLoggedIn) {
console.error(error);
}
if (error.status && error.status === 401 && storage) {
if (error.status && error.status === 401 && localStorage) {
// Unauthorized.
storage.removeItem('token');
localStorage.removeItem('token');
}
const errorMessage = error.translation_key
? t(`error.${error.translation_key}`)
@@ -11,7 +11,7 @@ class TalkProvider extends React.Component {
rest: this.props.rest,
graphql: this.props.graphql,
notification: this.props.notification,
storage: this.props.storage,
localStorage: this.props.localStorage,
history: this.props.history,
store: this.props.store,
};
@@ -30,7 +30,7 @@ TalkProvider.childContextTypes = {
rest: PropTypes.func,
graphql: PropTypes.object,
notification: PropTypes.object,
storage: PropTypes.object,
localStorage: PropTypes.object,
history: PropTypes.object,
store: PropTypes.object,
};
+5 -5
View File
@@ -64,8 +64,8 @@ export async function createContext({
init = noop,
} = {}) {
const eventEmitter = new EventEmitter({ wildcard: true });
const storage = createStorage();
const pymStorage = createPymStorage(pym);
const localStorage = createStorage('localStorage');
const pymLocalStorage = createPymStorage(pym, 'localStorage');
const history = createHistory(BASE_PATH);
const introspection = createIntrospection(introspectionData);
let store = null;
@@ -75,7 +75,7 @@ export async function createContext({
// NOTE: THIS IS ONLY EVER EVALUATED ONCE, IN ORDER TO SEND A DIFFERNT
// TOKEN YOU MUST DISCONNECT AND RECONNECT THE WEBSOCKET CLIENT.
return getAuthToken(store, storage);
return getAuthToken(store, localStorage);
};
const rest = createRestClient({
@@ -116,10 +116,10 @@ export async function createContext({
rest,
graphql,
notification,
storage,
localStorage,
history,
introspection,
pymStorage,
pymLocalStorage,
};
// Load framework fragments.
+1 -1
View File
@@ -68,7 +68,7 @@ function getLocale(storage) {
export function setupTranslations() {
// Setup the translation framework with the storage.
const storage = createStorage();
const storage = createStorage('localStorage');
const locale = getLocale(storage);
setLocale(storage, locale);
@@ -7,7 +7,7 @@ import {
const STORAGE_PATH = 'talkPluginRememberSort';
export default {
init: async ({ store, pymStorage, introspection }) => {
init: async ({ store, pymLocalStorage, introspection }) => {
// TODO: workaround as this plugin is included in any target and
// embeds (e.g. admin), but should only be included inside the stream.
@@ -16,10 +16,10 @@ export default {
return;
}
// We use pymStorage instead to persist the data directly on the parent page,
// We use pymLocalStorage instead to persist the data directly on the parent page,
// in order to mitigate strict cross domain security settings.
let sort = JSON.parse(await pymStorage.getItem(STORAGE_PATH));
let sort = JSON.parse(await pymLocalStorage.getItem(STORAGE_PATH));
if (
sort &&
introspection.isValidEnumValue('SORT_ORDER', sort.sortOrder) &&
@@ -35,7 +35,7 @@ export default {
// Save sorting choice to storage if it has changed.
if (!sort || sort.sortOrder !== sortOrder || sort.sortBy !== sortBy) {
sort = { sortOrder, sortBy };
pymStorage.setItem(STORAGE_PATH, JSON.stringify(sort));
pymLocalStorage.setItem(STORAGE_PATH, JSON.stringify(sort));
}
});
},