redux-persist-transform-encrypt icon indicating copy to clipboard operation
redux-persist-transform-encrypt copied to clipboard

How to update the secretKey async

Open rueiwoqp opened this issue 4 years ago • 6 comments

On app load, I want to do the following:

  1. Check expo-secure-store async for a previous stored encryption secret key
  2. If it doesn't exist, create a random uuid and save it in expo-secure-store async and use it as the secret key for encryption
  3. If it does exist, use it as the secret key for encryption

However, because this process is async, I don't see how to export the store and persistor.

const persistConfig = {
  key: 'root',
  version: 2,
  storage: FSStorage(),
  transforms: [
    encryptTransform({
      secretKey: 'my-super-secret-key',
      onError: function (error) {
        // Handle the error.
      },
    }),
  ],
};

export const store = configureStore({
  reducer: rootReducer,
  enhancers: [autoRehydrate()]
});

export const persistor = persistStore(store, persistConfig);

Does anyone have an example of how to update the secretKey after the app has loaded, without hard-coding it?

rueiwoqp avatar Mar 03 '21 17:03 rueiwoqp

hi, I have similar problem, I want to follow the recommendation for long-term persistence, but I am not sure how to set/change the secret key.

It seems the example configuration runs when the store is initialized, and I can't see a later point where I can change that.

If this could be added to the readme, that would be super useful.

Thanks

gczene avatar May 12 '21 19:05 gczene

@rueiwoqp I have the exact same problem at the moment as you described. What did you do to solve this?

Would be very useful to hear.

Thanks

andreaslydemann avatar Jun 09 '21 12:06 andreaslydemann

@rueiwoqp @gczene @andreaslydemann I've managed to do this. I think I will provide a more detailed example soon but for the moment, here are the guidelines:

  1. Make your configureStore function async
  2. Do not render the Provider until store creation is finished by putting the configureStore method in a useEffet at the root of your app.

ou2s avatar Apr 11 '22 09:04 ou2s

@ou2s +1 interested in this example!

gtokman avatar Apr 18 '22 13:04 gtokman

@ou2s can you provide the example for this asap.

keval2605 avatar Jun 15 '22 16:06 keval2605

Sorry for the late response. I ended up using another library because my code did not work as intended.

ou2s avatar Oct 06 '22 15:10 ou2s

I have the same question. The documentation recommends I should not generate or hard-code the secret key on the client side but does not show any example of how can I set/update the key after obtaining it from the server. Any solution for this? Thanks!

froston avatar Oct 20 '22 16:10 froston

I have the same question. The documentation recommends I should not generate or hard-code the secret key on the client side but does not show any example of how can I set/update the key after obtaining it from the server. Any solution for this? Thanks!

You can store the key in your .env file

ali-taghipour avatar Oct 20 '22 22:10 ali-taghipour

@ali-taghipour Thanks! That is definitelly a better approach then hard-coding the key. However this would only prevent to have the secret key in the code base not in the JS code itself. I guess that in order to avoid exposing the key in browser it needs to be dynamic, coming from the server in the runtime, which must be async.

froston avatar Oct 21 '22 14:10 froston