1

在 react-native 中使用 redux-persist 的 encrypt 选项似乎存在问题:

https://github.com/maxdeviant/redux-persist-transform-encrypt/issues/15

任何人都可以提供任何解决方案/解决方法来使用 redux persist 在 react-native 中加密和存储登录令牌吗?

当我尝试将 redux 与 redux-persist-transform-encrypt 一起使用时,我得到 Redux-persist-transform-encrypt: expected outbound state to be a string error

import { createStore, compose, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import { persistStore, autoRehydrate } from 'redux-persist';
import { AsyncStorage } from 'react-native';
import createEncryptor from 'redux-persist-transform-encrypt';
import reducers from './reducers';


const store = createStore(
  reducers,
  {},
  compose(
    applyMiddleware(ReduxThunk),
    autoRehydrate(),
  ),
);

const encryptor = createEncryptor({
  secretKey: 'my-super-secret-key-999',
});


persistStore(
  store,
  {
    storage: AsyncStorage,
    whitelist: ['auth'],
    transforms: [encryptor],
  },
);
export default store;

我的身份验证状态是这样的:

const INITIAL_STATE = {
  user: null,
  token: ''
};

在使用 redux persist 时,是否有任何解决方案可以使用 redux-persist-transform encrypt 或 transform 和其他包来加密令牌?

4

3 回答 3

4

我找到了一个使用 customTransform 而不是 redux-persist-transform-encrypt 的解决方案:

import { createStore, compose, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import { persistStore, createTransform, autoRehydrate } from 'redux-persist';
import { AsyncStorage } from 'react-native';
import CryptoJS from 'crypto-js';
import reducers from './reducers';


const store = createStore(
  reducers,
  {},
  compose(
    applyMiddleware(ReduxThunk),
    autoRehydrate(),
  ),
);

const encrypt = createTransform(
  (inboundState, key) => {
    if (!inboundState) return inboundState;
    const cryptedText = CryptoJS.AES.encrypt(JSON.stringify(inboundState), 'secret key 123');

    return cryptedText.toString(); 
  },
  (outboundState, key) => {
    if (!outboundState) return outboundState;
    const bytes = CryptoJS.AES.decrypt(outboundState, 'secret key 123');
    const decrypted = bytes.toString(CryptoJS.enc.Utf8);

    return JSON.parse(decrypted);
  },
);

persistStore(
  store,
  {
    storage: AsyncStorage,
    whitelist: ['auth'], // <-- keys from state that should be persisted
    transforms: [encrypt],
  },
);
export default store;

当使用 redux-persist 初始状态在补液完成之前触发,所以我也必须应用它:

https://github.com/rt2zz/redux-persist/blob/master/docs/recipes.md#delay-render-until-rehydration-complete

于 2017-07-20T16:52:54.667 回答
2

这对我有用:

import { createStore } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import createEncryptor from 'redux-persist-transform-encrypt';

import storage from 'redux-persist/lib/storage';
import rootReducer from '/path/to/your/rootReducer';

const encryptor = createEncryptor({
  secretKey: 'omg-this-is-some-secret-stuff',
});

const persistConfig = {
  key: 'root',
  storage,
  transforms: [encryptor],
};

const reducer = persistReducer(persistConfig, rootReducer);

export const store = createStore(reducer);
export const persistor = persistStore(store);
于 2018-03-31T07:46:34.203 回答
0
This is working fine and store data in session storage.
Use below to encript redux data.
1) install redux-persist-transform-encrypt module
yarn add redux-persist-transform-encrypt
npm install redux-persist-transform-encrypt --save
2) import redux files.
import { createStore } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import createEncryptor from 'redux-persist-transform-encrypt';
import storageSession from 'redux-persist/lib/storage/session';// store data in session storage
// import storage from 'redux-persist/lib/storage'; // store data in local storage
import promise from 'redux-promise';
import rootReducer from './path/reducers/';
const encryptor = createEncryptor({
  secretKey: 'test@key12',
});
const persistConfig = {
  key: 'root',
  storage:storageSession,
  // storage:storage,
  transforms: [encryptor],
}

const persistedReducer = persistReducer(persistConfig, rootReducer)

export default () => {
  let store = createStore(persistedReducer)
  let persistor = persistStore(store)
  return { store, persistor }
}
于 2019-05-09T17:16:51.573 回答